Csharp/C Sharp/XML LINQ/XDocument

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Create XDocument from XmlReader

<source lang="csharp"> using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Data.Linq; using System.Reflection; using System.Xml; using System.Xml.Linq; class Program {

   static void Main(string[] args) {
       XmlReader reader = XmlReader.Create("Employee.xml");
       XDocument xml = XDocument.Load(reader);
       Console.WriteLine(xml);
       XElement idperson = xml.Descendants("idP").Last();
       idperson.Add(new XElement("idperson",
                       new XAttribute("id", 1),
                       new XAttribute("year", 2006),
                       new XAttribute("salary", "16")));
       StringWriter sw = new StringWriter();
       XmlWriter w = XmlWriter.Create(sw);
       xml.Save(w);
       w.Close();
       Console.WriteLine(sw.ToString());
   }

}

</source>


Creating an XML Document with XDocument

<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass {

   public static void Main() {
       XDocument xDocument = new XDocument(
         new XDeclaration("1.0", "UTF-8", "yes"),
         new XDocumentType("Books", null, "Books.dtd", null),
         new XProcessingInstruction("Book", "out-of-print"),
         new XElement("Books"));
       Console.WriteLine(xDocument);
   }

}

</source>


Get Encoding, Version and standalone from XML declaration

<source lang="csharp"> using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Linq; using System.Reflection; using System.Xml.Linq; class Program {

   static void Main(string[] args) {
       XDocument xml = XDocument.Load("Hello_XLINQ.xml");
       Console.WriteLine("Encoding: {0}", xml.Declaration.Encoding);
       Console.WriteLine("Version: {0}", xml.Declaration.Version);
       Console.WriteLine("Standalone: {0}", xml.Declaration.Standalone);
   }

}

</source>


Intentionally Exposing the Halloween Problem

<source lang="csharp">

using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass {

   public static void Main() {
       XDocument xDocument = new XDocument(
         new XElement("Books",
         new XElement("Book",
         new XAttribute("type", "Author"),
         new XElement("FirstName", "J"),
         new XElement("LastName", "R")),
         new XElement("Book",
         new XAttribute("type", "Author"),
         new XElement("FirstName", "E"),
         new XElement("LastName", "B"))));
       IEnumerable<XElement> elements =
         xDocument.Element("Books").Elements("Book");
       foreach (XElement element in elements) {
           Console.WriteLine("Source element: {0} : value= {1}",element.Name, element.Value);
       }
       foreach (XElement element in elements) {
           Console.WriteLine("Removing {0}= {1} ¡�", element.Name, element.Value);
           element.Remove();
       }
       Console.WriteLine(xDocument);
   }

}

</source>


Loading a Document with the XDocument.Load Method with LoadOptions

<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass {

   public static void Main() {
       XDocument xDocument = XDocument.Load("book.xml",LoadOptions.SetBaseUri | LoadOptions.SetLineInfo);
       Console.WriteLine(xDocument);
       XElement firstName = xDocument.Descendants("FirstName").First();
       Console.WriteLine("FirstName Line:{0} Position:{1}",((IXmlLineInfo)firstName).LineNumber,((IXmlLineInfo)firstName).LinePosition);
       Console.WriteLine("FirstName Base URI:{0}", firstName.BaseUri);
   }

}

</source>


Query an XML document with Linq

<source lang="csharp"> using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Linq; using System.Reflection; using System.Xml.Linq; class Program {

   static void Main(string[] args) {
       XDocument xml = XDocument.Load(@"Employee.xml");
       var query = from p in xml.Elements("people").Elements("person")
                   where (int)p.Element("id") == 1
                   select p;
       foreach (var record in query) {
           Console.WriteLine("Employee: {0} {1}",
                                               record.Element("firstname").Value,
                                               record.Element("lastname").Value);
       }
   }

}

</source>


Saving a Document with the XDocument.Save Method

<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass {

   public static void Main() {
       XDocument xDocument = new XDocument(
        new XElement("Books",
          new XElement("Book",
          new XAttribute("type", "Author"),
          new XAttribute("experience", "first-time"),
          new XAttribute("language", "English"),
          new XElement("FirstName", "J"),
          new XElement("LastName", "R"))));
       xDocument.Save("book.xml");
   }

}

</source>


Traversing Forward from an XElement Object via the NextNode Property

<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass {

   public static void Main() {
       XElement firstParticipant;
       XDocument xDocument = new XDocument(
        new XDeclaration("1.0", "UTF-8", "yes"),
        new XDocumentType("Books", null, "Books.dtd", null),
       new XProcessingInstruction("Book", "out-of-print"),
          new XElement("Books", firstParticipant =
          new XElement("Book",
          new XAttribute("type", "Author"),
      new XElement("FirstName", "J"),
        new XElement("LastName", "R")),
        new XElement("Book",
        new XAttribute("type", "Author"),
        new XElement("FirstName", "E"),
        new XElement("LastName", "B"))));
       Console.WriteLine(firstParticipant.NextNode);
   }

}

</source>


Using the LINQ to XML API to Create an XML Document and Adding Some Structure

<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass {

   public static void Main() {
       XDocument xDocument =
        new XDocument(
        new XElement("Books",
        new XElement("Book",
        new XAttribute("type", "Author"),
        new XElement("FirstName", "J"),
        new XElement("LastName", "R"))));
       Console.WriteLine(xDocument.ToString());
   }

}

</source>