LINQ to XML查询

1. 读取XML文件

  XDocument和XElement类都提供了导入XML文件的Load()方法,可以读取XML文件的内容,并转换为XDocument或XElement类的实例。

  示例XML文件:Product.xml

<?xml version="1.0" encoding="utf-8"?>
<Products>
  <Product>
    <ProductID>1</ProductID>
    <ProductName>LINQ to XML</ProductName>
    <UnitPrice>10</UnitPrice>
  </Product>
  <Product>
    <ProductID>2</ProductID>
    <ProductName>LINQ to SQL</ProductName>
    <UnitPrice>20</UnitPrice>
  </Product>
</Products>
XElement root = XElement.Load(Server.MapPath("Product.xml"));

2. 查询根元素

XDocument doc = XDocument.Load(Server.MapPath("Product.xml"));

IEnumerable<XElement> elements = from e in doc.Elements("Products")
                                 select e;

foreach (XElement e in elements)
{
    Console.WriteLine("{0}-{1}", e.Name, e.Value);
}
XDocument doc = XDocument.Load(Environment.CurrentDirectory + @"\Product.xml");
IEnumerable<XElement> elements = from e in doc.Elements("Products")
                                 select e;
foreach (XElement e in elements)
{
    Console.WriteLine("{0}-{1}", e.Name, e.Value);
}

3.  查询节点

XDocument doc = XDocument.Load(Environment.CurrentDirectory + @"\" + "Product.xml");
var query = from p in doc.Element("Products").Elements("Product")
            where (int)p.Element("ProductID") == 1
            select p;

query.ToList().ForEach(item =>
{
    Console.WriteLine("{0}-{1}-{2}", item.Element("ProductID").Value, item.Element("ProductName").Value, item.Element("UnitPrice").Value);
});
XElement xml = XElement.Load(Environment.CurrentDirectory + @"\" + "Product.xml");
var query = from p in xml.Elements("Product")
            where (int)p.Element("ProductID") == 1
            select p;

4. 查询子孙节点

  Descendants轴方法与Elements类型,不过Elements只查找当前元素下的直接子节点,而Descendants则会遍历当前元素下任意层级的子元素。

  示例xml文件:

<?xml version="1.0" encoding="utf-8"?>
<Products>
  <Product>
    <Book>
      <ProductID>1</ProductID>
      <ProductName>LINQ to XML</ProductName>
      <UnitPrice>10</UnitPrice>
    </Book>
  </Product>
  <Product>
    <Book>
      <ProductID>2</ProductID>
      <ProductName>LINQ to SQL</ProductName>
      <UnitPrice>20</UnitPrice>
    </Book>
  </Product>
</Products>

  使用Descendants()方法查询:

XElement root = XElement.Load(Environment.CurrentDirectory + @"\" + "Product.xml");
var query = from b in root.Descendants("Book")
            select b;
foreach (var item in query)
{
    Console.WriteLine(item.Element("ProductName").Value);
}

5. 查询属性

  示例xml文件:

<?xml version="1.0" encoding="utf-8"?>
<Products>
  <Product ID="1">
    <ProductID>1</ProductID>
    <ProductName>LINQ to XML</ProductName>
    <UnitPrice>10</UnitPrice>
  </Product>
  <Product ID="2">
    <ProductID>2</ProductID>
    <ProductName>LINQ to SQL</ProductName>
    <UnitPrice>20</UnitPrice>
  </Product>
</Products>
XElement xml = XElement.Load(Environment.CurrentDirectory + @"\" + "Product.xml");
var query = from p in xml.Elements("Product")
            where (int)p.Attribute("ID") == 1
            select p;
XElement xml = XElement.Load(Environment.CurrentDirectory + @"\" + "Product.xml");
var query = from p in xml.Elements("Product")
            where (int)p.Attribute("ID") == 1
            select new
            {
                ID = p.Attribute("ID").Value,
                ProductID = p.Element("ProductID").Value,
                ProductName = p.Element("ProductName").Value
            };
XElement xml = XElement.Load(Environment.CurrentDirectory + @"\" + "Product.xml");
var query = from p in xml.Nodes().OfType<XElement>()
            where (int)p.Attribute("ID") == 1
            select new
            {
                ID = p.Attribute("ID").Value,
                ProductID = p.Element("ProductID").Value,
                ProductName = p.Element("ProductName").Value
            };
posted @ 2020-09-27 16:17  飞雪舞剑  阅读(73)  评论(0编辑  收藏  举报