Csharp/CSharp Tutorial/XML LINQ/XDocument — различия между версиями

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

Версия 18:31, 26 мая 2010

LINQ To XML Element Text

<source lang="csharp">using System; using System.Collections.Generic; using System.Linq; using System.Xml.Linq; using System.Text;

   class Program{
       static void Main(string[] args){
           XDocument xdoc = new XDocument(
               new XElement("customers",
                   new XElement("customer",
                       "AAAAAA",
                       new XAttribute("City", "New York"),
                       new XAttribute("Region", "North America")
                       ),
                   new XElement("customer",
                       "BBBBBB",
                       new XAttribute("City", "Mumbai"),
                       new XAttribute("Region", "Asia")
                   )
               )
           );
           Console.WriteLine(xdoc);
       }
   }</source>

Save and Load XML

<source lang="csharp">using System; using System.Collections.Generic; using System.Linq; using System.Xml.Linq; using System.Text;

   class Program
   {
       static void Main(string[] args)
       {
           XDocument xdoc = new XDocument(
               new XElement("customers",
                   new XElement("customer",
                       new XAttribute("ID", "A"),
                       new XAttribute("City", "New York"),
                       new XAttribute("Region", "North America"),
                       new XElement("order",
                           new XAttribute("Item", "Widget"),
                           new XAttribute("Price", 100)
                     ),
                     new XElement("order",
                       new XAttribute("Item", "Tire"),
                       new XAttribute("Price", 200)
                     )
                   )
               )
           );
           string xmlFileName = "e.xml";
           xdoc.Save(xmlFileName);
           XDocument xdoc2 = XDocument.Load(xmlFileName);
           Console.WriteLine(xdoc2);
       }
   }</source>