Csharp/CSharp Tutorial/XML LINQ/Query

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

Query XML: All descendants in document

<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)
       {
           string xmlFileName = "e.xml";
           XDocument customers = XDocument.Load(xmlFileName);
           var queryResult = from c in customers.Descendants() select c.Name;
           foreach (var item in queryResult)
           {
               Console.WriteLine(item);
           }
       }
   }</source>

Query XML: All distinct descendants in document

<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)
       {
           string xmlFileName = "e.xml";
           XDocument customers = XDocument.Load(xmlFileName);
           var queryResult = from c in customers.Descendants() select c.Name;
           foreach (var item in queryResult.Distinct())
           {
               Console.WriteLine(item);
           }
       }
   }</source>

Query XML: Attributes named "Company"

<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)
       {
           string xmlFileName = "e.xml";
           XDocument customers = XDocument.Load(xmlFileName);
           var queryResult =
               from c in customers.Descendants("customer").Attributes("Company")
               select c.Name;
           foreach (var item in queryResult)
           {
               Console.WriteLine(item);
           }
       }
   }</source>

Query XML: attributes named "orderYear"

<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)
       {
           string xmlFileName = "e.xml";
           XDocument customers = XDocument.Load(xmlFileName);
           var queryResults =
               from c in customers.Descendants("order").Attributes("orderYear")
               select c;
           foreach (var item in queryResults)
           {
               Console.WriteLine(item);
           }                
       }
   }</source>

Query XML: Attributes of descendants named "customer"

<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)
       {
           string xmlFileName = "e.xml";
           XDocument customers = XDocument.Load(xmlFileName);
           var queryResult = from c in customers.Descendants("customer").Attributes() select c.Name;
           foreach (var item in queryResult)
           {
               Console.WriteLine(item);
           }
       }
   }</source>

Query XML by select an element

<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)
       {
           string xmlFileName = "e.xml";
           XDocument customers = XDocument.Load(xmlFileName);
           var queryResult = from c in customers.Elements() select c.Name;
           foreach (var item in queryResult)
           {
               Console.WriteLine(item);
           }
       }
   }</source>

Query XML: Descendants named "customer"

<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)
       {
           string xmlFileName = "e.xml";
           XDocument customers = XDocument.Load(xmlFileName);
           var queryResult = from c in customers.Descendants("customer")  select c.Name;
           foreach (var item in queryResult)
           {
               Console.WriteLine(item);
           }
       }
   }</source>

Query XML: Earliest year in which orders were placed

<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)
       {
           string xmlFileName = "e.xml";
           XDocument customers = XDocument.Load(xmlFileName);
           var queryResults =
               from c in customers.Descendants("order").Attributes("orderYear")
               select c.Value;                      
           Console.WriteLine("Earliest year in which orders were placed: {0}", queryResults.Min());
          
       }
   }</source>

Query XML: Values of customer attributes named "Company"

<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)
       {
           string xmlFileName = "e.xml";
           XDocument customers = XDocument.Load(xmlFileName);
           var queryResults =
               from c in customers.Descendants("customer").Attributes("Company")
               select c;
           foreach (var item in queryResults)
           {
               Console.WriteLine(item);
           }
       }
   }</source>

Query XML: Values of descendant attributes named "orderYear"

<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)
       {
           string xmlFileName = "e.xml";
           XDocument customers = XDocument.Load(xmlFileName);
           var queryResults =
               from c in customers.Descendants("order").Attributes("orderYear")
               select c.Value;
           foreach (var item in queryResults)
           {
               Console.WriteLine(item);
           }              
       }
   }</source>

Using Linq To query Xml

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

   class LinqToXml
   {
       static void Main()
       {
           XDocument doc = XDocument.Load("data.xml");
           var filtered = from p in doc.Descendants("Product")
                          join s in doc.Descendants("Supplier")
                          on (int)p.Attribute("SupplierID")
                              equals (int)s.Attribute("SupplierID")
                          where (decimal)p.Attribute("Price") > 10
                          orderby (string)s.Attribute("Name"),
                                  (string)p.Attribute("Name")
                          select new
                          {
                              SupplierName = (string)s.Attribute("Name"),
                              ProductName = (string)p.Attribute("Name")
                          };
           foreach (var v in filtered)
           {
               Console.WriteLine("Supplier={0}; Product={1}",
                                 v.SupplierName, v.ProductName);
           }
       }
   }

//File: data.xml /* <?xml version="1.0"?>

 <Products>
   <Product Name="E" Price="9.99" SupplierID="1" />
   <Product Name="A" Price="14.99" SupplierID="2" />
   <Product Name="C" Price="13.99" SupplierID="1" />
   <Product Name="D" Price="10.99" SupplierID="3" />
 </Products>

  • /</source>