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 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. "Version" public string[] Values { get; set; } //z.B. "NX 1953" public string Standard { get; set; } //z.B. "yes" public void Load() { // Das Dokument in eine neue XElement-Instanz laden XElement rootElement = XElement.Load(@"C:\Temp\Test\Test.xml"); // 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) { //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.Version = keyAttribut.Value; } //Standard-Element suchen und speichern XAttribute standardAttribut = groupElement.Attribute("Standard"); if (standardAttribut != null) { group.Standard = standardAttribut.Value; } } } public void LoadData() { //XElement zum Lesen erzeugen XElement personsXML = XElement.Load(FileName); //XDocument personsXML = XDocument.Load(FileName); //Alle Aliens abfragen var persons = from person in personsXML.Descendants("person") where person.Element("type").Value == "Alien" select new { Id = (int)person.Attribute("id"), FirstName = person.Element("firstname").Value, LastName = person.Element("lastname").Value }; //Die abgefragten Personen durchgehen und derern Daten ausgeben foreach (var person in persons) { //TODO: } } } class Topics { public string Id; public string Version; public string Standard; } }