xml_example

CSharp · Console

/Program.cs

using System;
using System.Linq;
using System.Xml.Linq;
					
public class Program
{
	static public void Main(string[] args)
        {
		    XDocument xml = new XDocument(  
		     new XElement("settings",  
             new XElement("Datum", "9 Januar"),  
             new XElement("Wert1", "eins"),  
             new XElement("Wert2", "zwei"),  
             new XElement("Wert3", "drei"),  
             new XElement("Wert4", "vier")
            )  
         );  
         
            var xmlQuery = from item in xml.Descendants("settings")
                           select item;

            foreach (XElement items in xmlQuery)
            {
			  try {
                Console.WriteLine(items.Element("Datum").Value);
                Console.WriteLine(items.Element("Wert1").Value);
                Console.WriteLine(items.Element("Wert2").Value);
                Console.WriteLine(items.Element("Wert3").Value);
                Console.WriteLine(items.Element("Wert4").Value);
			  } catch(Exception e) {
			    Console.WriteLine("Error: {0}",e);
			  }
            }
            Console.ReadLine();
        }
    }
;