singlevaluecontrol_user_con.../SingleValueControl/LoadXML.cs
Eugen Höglinger 4e699b9a86 Initialize
- Control zum Laden, Bearbeiten und Speichern von XML-Daten
2022-12-20 09:31:36 +01:00

171 lines
5.6 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
namespace Eugen.ESystem.Windows.Forms
{
internal class LoadXML
{
public string FileName { get; set; }
public string Topic { get; set; } //z.B. "NX-Portal"
public string Group { get; set; } //z.B. "NX"
public string Key { get; set; } //z.B. "Key"
public string[] Values { get; set; } //z.B. "NX 1953"
public int Standard { get; set; } //z.B. "2"
public void Load()
{
if (File.Exists(FileName))
{
// Das Dokument in eine neue XElement-Instanz laden
XElement rootElement = XElement.Load(FileName);
// Auflistung für die Gruppe erzeugen
List<Topics> groups = new List<Topics>();
//Alle groups-Elemente einlesen und durchgehen
//var personElements = rootElement.Elements(Group);
var groupElements = rootElement.Elements();
if (String.IsNullOrEmpty(Group))
{
string[] temp = new string[5];
List<string> lkeys = new List<string>();
List<string> lvalues = new List<string>();
List<string> lstandard = new List<string>();
foreach (var groupElement in groupElements)
{
temp = groupElement.ToString().Split(' ');
//Wenn Leerzeichen im Wert sind, dann werden mehr als 6 Elemente belegt.
//In diesem Fall die Teilelemente wieder zu einem Element zusammenführen.
if (temp.Length > 6)
{
int count = temp.Length - 6;
for (int i = 1; i < count + 1; i++)
{
temp[2] += " " + temp[2 + i]; //Die Teilelemente zusammensetzen
}
var numbersList = temp.ToList(); //Array in eine List umwandeln
for (int i = 0; i < count; i++)
{
numbersList.Remove(numbersList[3]); //Die nicht mehr gültigen Teilelemente löschen
}
temp = numbersList.ToArray(); //List in ein Array zurückwandeln
}
Group = ExtractGroup(temp);
Key = ExtractKey(temp);
lvalues.Add(ExtractValue(temp));
lstandard.Add(ExtractStandard(temp));
}
string[] values = new string[lvalues.Count];
int standard = 0;
for (int i = 0; i < lvalues.Count; i++)
{
values[i] = lvalues[i];
if (lstandard[i] == "yes")
{
standard = i + 1;
}
}
Values = values;
Standard = standard;
}
else
{
foreach (var groupElement in groupElements)
{
//Neue Gruppe erzeugen und in der Auflistung ablegen
Topics group = new Topics();
groups.Add(group);
//Das Attribut id einlesen
XAttribute idAttribute = groupElement.Attribute("Id");
if (idAttribute != null)
{
group.Id = idAttribute.Value;
}
//Key-Element suchen und speichern
XAttribute keyAttribut = groupElement.Attribute(Key);
if (keyAttribut != null)
{
group.Key = keyAttribut.Value;
}
//Standard-Element suchen und speichern
XAttribute standardAttribut = groupElement.Attribute("Standard");
if (standardAttribut != null)
{
group.Standard = standardAttribut.Value;
}
}
DataPreperation(groups);
}
}
}
private string ExtractGroup(string[] temp)
{
return temp[0].Remove(0, 1);
}
private string ExtractKey(string[] temp)
{
return temp[2].Remove(temp[2].IndexOf("="));
}
private string ExtractValue(string[] temp)
{
return temp[2].Remove(0, temp[2].IndexOf("=") + 1).Replace("\"", "");
}
private string ExtractStandard(string[] temp)
{
return temp[3].Remove(0, temp[3].IndexOf("=") + 1).Replace("\"", "");
}
private void DataPreperation(List<Topics> groups)
{
string[] values = new string[groups.Count];
for (int i = 0; i < groups.Count; i++)
{
values[i] = groups[i].Key;
if (groups[i].Standard == "yes")
{
Standard = i + 1;
}
}
Values = values;
}
}
class Topics
{
public string Id;
public string Key;
public string Standard;
}
}