Материал из .Net Framework эксперт
Query XML: All descendants in document
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);
}
}
}
Query XML: All distinct descendants in document
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);
}
}
}
Query XML: Attributes named "Company"
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);
}
}
}
Query XML: attributes named "orderYear"
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);
}
}
}
Query XML: Attributes of descendants named "customer"
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);
}
}
}
Query XML by select an element
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);
}
}
}
Query XML: Descendants named "customer"
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);
}
}
}
Query XML: Earliest year in which orders were placed
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());
}
}
Query XML: Values of customer attributes named "Company"
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);
}
}
}
Query XML: Values of descendant attributes named "orderYear"
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);
}
}
}
Using Linq To query Xml
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"?>
<Data>
<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>
</Data>
*/