4.Linq to Xml

目录

1.Linq to Xml函数构造方法

2.创建包含文本节点的Xml文档

3.保存和加载Xml

4.处理Xml片段

5.从数据库中生成XML

 

 

 

1.Linq to Xml函数构造方法

Linq to Xml引入了一种创建xml的方式,叫做函数构建方式(functional construction),通过这种方式可以以一种类似Xml文档结构的方式快速构建XML。

            //使用Linq To XML 函数构建方式创建XML文档
            XDocument xdoc = new XDocument(
                new XElement("customers",
                    new XElement("customer",
                        new XAttribute("ID", "A"),
                        new XAttribute("City", "New York"),
                        new XAttribute("Region", "USA"),
                        new XElement("order",
                            new XAttribute("Item", "Widget"),
                            new XAttribute("Price", 100)
                        ),
                        new XElement("order",
                            new XAttribute("Item", "Tire"),
                            new XAttribute("Price", 200)
                        )
                    ),
                    new XElement("customer",
                        new XAttribute("ID", "B"),
                        new XAttribute("City", "Mumbai"),
                        new XAttribute("Region", "Asia"),
                        new XElement("order",
                            new XAttribute("Item", "Oven"),
                            new XAttribute("Price", 501)
                        )
                    )
                )
            );

            Console.WriteLine(xdoc2.ToString());

2.创建包含文本节点的Xml文档

            //使用字符串构建包含文本节点的XML文档
            XDocument xdocContent = new XDocument(
               new XElement("customers",
                   new XElement("customer",
                       new XAttribute("ID", "A"),
                       new XAttribute("City", "New York"),
                       new XAttribute("Region", "USA"),
                      "AAA"
                   ),
                   new XElement("customer",
                       new XAttribute("ID", "B"),
                       new XAttribute("City", "Mumbai"),
                       new XAttribute("Region", "Asia"),
                      "BBB"
                   )
               )
           );
            Console.WriteLine(xdocContent.ToString());

3.保存和加载Xml

从文件加载

4.Linq to Xml
            //使用Linq To XML 函数构建方式创建XML文档
            XDocument xdoc = new XDocument(
                new XElement("customers",
                    new XElement("customer",
                        new XAttribute("ID", "A"),
                        new XAttribute("City", "New York"),
                        new XAttribute("Region", "USA"),
                        new XElement("order",
                            new XAttribute("Item", "Widget"),
                            new XAttribute("Price", 100)
                        ),
                        new XElement("order",
                            new XAttribute("Item", "Tire"),
                            new XAttribute("Price", 200)
                        )
                    ),
                    new XElement("customer",
                        new XAttribute("ID", "B"),
                        new XAttribute("City", "Mumbai"),
                        new XAttribute("Region", "Asia"),
                        new XElement("order",
                            new XAttribute("Item", "Oven"),
                            new XAttribute("Price", 501)
                        )
                    )
                )
            );

            //从文件加载
            string fileName = "xmltest.xml";
            xdoc.Save(fileName);//保存生成的xml文档
            XDocument xdoc2 = XDocument.Load(fileName);//加载XML
            Console.WriteLine(xdoc2.ToString());
View Code

从字符串加载

4.Linq to Xml
            //注:ID=""A""使用双引号只是为了在@字符串中包含引号,也可以使用转义如ID=\"A\"
            string xmlContent = @"
                <customers>
                    <customer ID=""A"" City=""New York"" Region=""USA"">AAA</customer>
                    <customer ID=""B"" City=""Mumbai"" Region=""Asia"">BBB</customer>
                </customers>            
                ";

            //使用Parse从字符串加载XML
            XDocument xdoc3 = XDocument.Parse(xmlContent);
View Code

4.处理Xml片段

处理Xml片段和处理Xml文档一样,只不过把Element当作*元素。xml片段可以应用到xml文档或者别的xml片段

            //处理XML片段
            XElement xcust = new XElement("customer",
                        new XAttribute("ID", "C"),
                        new XAttribute("City", "New York"),
                        new XAttribute("Region", "USA"),
                        new XElement("order",
                            new XAttribute("Item", "Widget"),
                            new XAttribute("Price", 600)
                        ),
                        new XElement("order",
                            new XAttribute("Item", "Tire"),
                            new XAttribute("Price", 700)
                        )
                    );
            string xcustFilePath = "xcust.xml";
            xcust.Save(xcustFilePath);

            XElement xcust2 = XElement.Load(xcustFilePath);
            xdoc.Root.Add(xcust2);//xml片段加到xml文档
            Console.WriteLine(xcust2);

 5.从数据库中生成XML

从数据库中生成XML先添加ADO.NET实体数据模型CustomerOrderEntities,然后利用添加的实体数据模型生成XML

            CustomerOrderEntities entity = new CustomerOrderEntities();
            XElement xdocSql = new XElement("Customers",
                from n in entity.tblCustomer.AsEnumerable()
                where n.Region != "Asia"
                select new XElement("Customer", new XAttribute("ID", n.ID),
                    new XAttribute("CustomerName", n.CustomerName),
                    from o in n.btlOrder
                    select new XElement("Order", new XAttribute("OrderId", o.OrderId),
                       new XAttribute("SKU", o.SKU),
                       new XAttribute("Qty", o.Qty)
                    )));
            Console.WriteLine(xdocSql.ToString());    

生成的XML如下

<Customers>
  <Customer ID="1" CustomerName="tom">
    <Order OrderId="111-101" SKU="sku1" Qty="1" />
    <Order OrderId="111-102" SKU="sku2" Qty="2" />
  </Customer>
</Customers>

6.查询XML

//使用Linq To XML 函数构建方式创建XML文档
            XDocument xdoc = new XDocument(
                new XElement("customers",
                    new XElement("customer",
                        new XAttribute("ID", "A"),
                        new XAttribute("City", "New York"),
                        new XAttribute("Region", "USA"),
                        new XElement("order",
                            new XAttribute("Item", "Widget"),
                            new XAttribute("Price", 100)
                        ),
                        new XElement("order",
                            new XAttribute("Item", "Tire"),
                            new XAttribute("Price", 200)
                        )
                    ),
                    new XElement("customer",
                        new XAttribute("ID", "B"),
                        new XAttribute("City", "Mumbai"),
                        new XAttribute("Region", "Asia"),
                        new XElement("order",
                            new XAttribute("Item", "Oven"),
                            new XAttribute("Price", 501)
                        )
                    )
                )
            );
XElement queryResult = (from UserInfo in xdoc.Root.Elements("customer") where UserInfo.Attribute("ID").Value == "A" select UserInfo).SingleOrDefault();

返回的xml节点如下

<customer ID="A" City="New York" Region="USA">
  <order Item="Widget" Price="100" />
  <order Item="Tire" Price="200" />
</customer>

Elements()方法返回xml文档或者xml片段的所有一级元素,也可以传递节点名称返回该名称的所有一级元素

Descendants()返回所有级别的子元素,与之相反.Ancestors()返回当前节点以上的所有元素

Attributes()返回所有属性节点,Attributes("ID")返回所有属性为ID的属性节点,如 xdoc.Root.Elements("customer").Attributes("ID")返回元素节点customer的所有ID属性节点

Attribute("ID")返回ID属性节点

 

上一篇:如何将XElement转换为XComment(C#)


下一篇:xml文件读写