196 lines
7.2 KiB
C#
196 lines
7.2 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
|
|
namespace Test_LoadXMLFile
|
|
{
|
|
internal class LoadXML
|
|
{
|
|
public enum LoadAll
|
|
{
|
|
No,
|
|
Yes
|
|
}
|
|
|
|
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 LoadAll LoadAllValues { get; set; }
|
|
|
|
public void Load()
|
|
{
|
|
if (File.Exists(FileName))
|
|
{
|
|
// Das Dokument in eine neue XElement-Instanz laden
|
|
XElement rootElement = XElement.Load(FileName);
|
|
|
|
if (rootElement.ToString().Remove(rootElement.ToString().IndexOf(" ")) == "<" + Topic || LoadAllValues == LoadAll.Yes)
|
|
{
|
|
// 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)
|
|
{
|
|
//Nur wenn das 'groupElement' so beginnt wie 'Group' heißt, dann laden
|
|
if (groupElement.ToString().Remove(groupElement.ToString().IndexOf(" ")) == "<" + Group || LoadAllValues == LoadAll.Yes)
|
|
{
|
|
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, 3];
|
|
int standard = 0;
|
|
|
|
for (int i = 0; i < lvalues.Count; i++)
|
|
{
|
|
values[i, 2] = lvalues[i];
|
|
|
|
if (lstandard[i] == "yes")
|
|
{
|
|
standard = i + 1;
|
|
}
|
|
}
|
|
|
|
Values = values;
|
|
Standard = standard;
|
|
}
|
|
else
|
|
{
|
|
foreach (var groupElement in groupElements)
|
|
{
|
|
//Nur wenn das 'groupElement' so beginnt wie 'Group' heißt, dann laden
|
|
if (groupElement.ToString().Remove(groupElement.ToString().IndexOf(" ")) == "<" + Group || LoadAllValues == LoadAll.Yes)
|
|
{
|
|
//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);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Values = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
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, 3];
|
|
|
|
for (int i = 0; i < groups.Count; i++)
|
|
{
|
|
values[i, 0] = groups[i].Id;
|
|
values[i, 1] = groups[i].Key;
|
|
values[i, 2] = groups[i].Standard;
|
|
|
|
if (groups[i].Standard == "yes")
|
|
{
|
|
Standard = i + 1;
|
|
}
|
|
}
|
|
|
|
Values = values;
|
|
}
|
|
}
|
|
|
|
class Topics
|
|
{
|
|
public string Id;
|
|
public string Key;
|
|
public string Standard;
|
|
}
|
|
}
|