目录

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如下

<Customers>
<Customer ID="" CustomerName="tom">
<Order OrderId="111-101" SKU="sku1" Qty="" />
<Order OrderId="111-102" SKU="sku2" Qty="" />
</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", )
),
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节点如下

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

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

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

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

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

4.Linq to Xml的更多相关文章

  1. LINQ系列:LINQ to XML类

    LINQ to XML由System.Xml.Linq namespace实现,该namespace包含处理XML时用到的所有类.在使用LINQ to XML时需要添加System.Xml.Linq. ...

  2. LINQ系列:LINQ to XML操作

    LINQ to XML操作XML文件的方法,如创建XML文件.添加新的元素到XML文件中.修改XML文件中的元素.删除XML文件中的元素等. 1. 创建XML文件 string xmlFilePath ...

  3. LINQ系列:LINQ to XML查询

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

  4. Linq to Xml读取复杂xml(带命名空间)

    前言:xml的操作方式有多种,但要论使用频繁程度,博主用得最多的还是Linq to xml的方式,觉得它使用起来很方便,就用那么几个方法就能完成简单xml的读写.之前做的一个项目有一个很变态的需求:C ...

  5. [原创]Linq to xml增删改查Linq 入门篇:分分钟带你遨游Linq to xml的世界

    本文原始作者博客 http://www.cnblogs.com/toutou Linq 入门篇(一):分分钟带你遨游linq to xml的世界 本文原创来自博客园 请叫我头头哥的博客, 请尊重版权, ...

  6. c#操作xml文件(XmlDocument,XmlTextReader,Linq To Xml)

    主界面

  7. Linq对XML的简单操作

    前两章介绍了关于Linq创建.解析SOAP格式的XML,在实际运用中,可能会对xml进行一些其它的操作,比如基础的增删该查,而操作对象首先需要获取对象,针对于DOM操作来说,Linq确实方便了不少,如 ...

  8. LINQ to XML 编程基础

    1.LINQ to XML类 以下的代码演示了如何使用LINQ to XML来快速创建一个xml: 隐藏行号 复制代码 ?创建 XML public static void CreateDocumen ...

  9. XML基础学习02<linq to xml>

    Linq to XML的理解 1:这是一种比较好的操作Xml的工具. àXDocument 文档 àXElement 元素 àXAttribute 属性 àXText 文本 2:这里还是和我们之前创建 ...

  10. C# ~ 从 XML 到 Linq 到 Linq to XML

    .XML 可扩展标记语言 (Extensible Markup Language), 标记 (markup) 是关键部分,是标准通用标记语言 (Standard Generalized Markup ...

随机推荐

  1. 【视频开发】CximageMat 、CximagelplImage 以及 lplImageMat的转换、像素位深度

    1.传统的lplImage * -------> Mat格式 IplImage* img = cvLoadImage("greatwave.png", 1); Mat mtx ...

  2. Indy10 Tcp接收数据问题

    在做Delphi开发时,使用Indy组件来做网络通讯是一种比较快捷的方式.今天要说一下indy10中tcp接收数据的问题. 我们在测试时经常使用Wrinteln来发送数据,用Readln来接收数据.用 ...

  3. python3黑帽子渗透笔记第二章--网络基础

    1 先来看看不可少的socket模块 (1)tcp_client.py 在渗透测试过程中,创建一个tcp客户端连接服务,发送垃圾数据,进行模糊测试等. (2)udp_client.py 2 nc工具的 ...

  4. springboot集成mybatisplus小例子

    集成mybatisplus后,简单的CRUD就不用写了,如果没有特别的sql,就可以不用mapper的xml文件的. 目录 pom.xml文件 <?xml version="1.0&q ...

  5. Maven专题

    Maven 教程之 settings.xml 详解

  6. [转帖]Socat 入门教程

    https://www.hi-linux.com/posts/61543.html 现在安装k8s 必须带 socat 今天看一下socat 到底是啥东西呢. Socat 是 Linux 下的一个多功 ...

  7. 【转帖】国产PCIe SSD主控芯片获得中国芯大奖 3500MB/s读取

    国产PCIe SSD主控芯片获得中国芯大奖 3500MB/s读取 https://www.cnbeta.com/articles/tech/906033.htm 国产主控 在日前的2019“中国芯”集 ...

  8. KAFA架构及其基本概念

    1.目标 - KAFA价格 在我们上一篇Kafka教程中,我们讨论了Kafka用例和应用程序.今天,在这个Kafka教程中,我们将讨论Kafka Architecture.在这篇Kafka Archi ...

  9. Python中的条件判断、循环以及循环的终止

    条件判断 条件语句是用来判断给定条件是否满足,并根据判断所得结果从而决定所要执行的操作,通常的逻辑思路如下图: 单次判断 形式 if <判断条件>: <执行> else: &l ...

  10. Java 总结篇2

    第02章:数据类型和运算符 一.概述: 1.数据类型:int.float.char.boolean 2.运算符:算术运算符.赋值运算符.关系运算符.逻辑运算符.位运算符(了解即可).条件运算符 3.基 ...