4.Linq to Xml
阅读原文时间:2023年07月17日阅读:2

目录

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", )  
                    ),  
                    new XElement("order",  
                        new XAttribute("Item", "Tire"),  
                        new XAttribute("Price", )  
                    )  
                ),  
                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", )  
                    )  
                )  
            )  
        );

        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

从文件加载

        //使用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", )  
                    ),  
                    new XElement("order",  
                        new XAttribute("Item", "Tire"),  
                        new XAttribute("Price", )  
                    )  
                ),  
                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", )  
                    )  
                )  
            )  
        );

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

从字符串加载

        //注: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);

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", )  
                    ),  
                    new XElement("order",  
                        new XAttribute("Item", "Tire"),  
                        new XAttribute("Price", )  
                    )  
                );  
        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如下

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", )
),
new XElement("order",
new XAttribute("Item", "Tire"),
new XAttribute("Price", )
)
),
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", )
)
)
)
);
XElement queryResult = (from UserInfo in xdoc.Root.Elements("customer") where UserInfo.Attribute("ID").Value == "A" select UserInfo).SingleOrDefault();

返回的xml节点如下

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

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

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

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