using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; using System.Xml.Linq; using System.Xml.XPath; namespace Test_LoadXMLFile { 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. "Version" public string[,] Values { get; set; } //z.B. "NX 1953" public int Standard { get; set; } //z.B. "2" public void Load() { // Das Dokument in eine neue XElement-Instanz laden XElement rootElement = XElement.Load(FileName); if (rootElement.ToString().Remove(rootElement.ToString().IndexOf(" ")) == "<" + Topic) { // Auflistung für die Gruppe erzeugen List groups = new List(); //Alle groups-Elemente einlesen und durchgehen //var personElements = rootElement.Elements(Group); var groupElements = rootElement.Elements(); 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) { //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 void DataPreperation(List 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; } Values = values; } } class Topics { public string Id; public string Key; public string Standard; } }