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", )
),
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的更多相关文章
- LINQ系列:LINQ to XML类
LINQ to XML由System.Xml.Linq namespace实现,该namespace包含处理XML时用到的所有类.在使用LINQ to XML时需要添加System.Xml.Linq. ...
- LINQ系列:LINQ to XML操作
LINQ to XML操作XML文件的方法,如创建XML文件.添加新的元素到XML文件中.修改XML文件中的元素.删除XML文件中的元素等. 1. 创建XML文件 string xmlFilePath ...
- LINQ系列:LINQ to XML查询
1. 读取XML文件 XDocument和XElement类都提供了导入XML文件的Load()方法,可以读取XML文件的内容,并转换为XDocument或XElement类的实例. 示例XML文件: ...
- Linq to Xml读取复杂xml(带命名空间)
前言:xml的操作方式有多种,但要论使用频繁程度,博主用得最多的还是Linq to xml的方式,觉得它使用起来很方便,就用那么几个方法就能完成简单xml的读写.之前做的一个项目有一个很变态的需求:C ...
- [原创]Linq to xml增删改查Linq 入门篇:分分钟带你遨游Linq to xml的世界
本文原始作者博客 http://www.cnblogs.com/toutou Linq 入门篇(一):分分钟带你遨游linq to xml的世界 本文原创来自博客园 请叫我头头哥的博客, 请尊重版权, ...
- c#操作xml文件(XmlDocument,XmlTextReader,Linq To Xml)
主界面
- Linq对XML的简单操作
前两章介绍了关于Linq创建.解析SOAP格式的XML,在实际运用中,可能会对xml进行一些其它的操作,比如基础的增删该查,而操作对象首先需要获取对象,针对于DOM操作来说,Linq确实方便了不少,如 ...
- LINQ to XML 编程基础
1.LINQ to XML类 以下的代码演示了如何使用LINQ to XML来快速创建一个xml: 隐藏行号 复制代码 ?创建 XML public static void CreateDocumen ...
- XML基础学习02<linq to xml>
Linq to XML的理解 1:这是一种比较好的操作Xml的工具. àXDocument 文档 àXElement 元素 àXAttribute 属性 àXText 文本 2:这里还是和我们之前创建 ...
- C# ~ 从 XML 到 Linq 到 Linq to XML
.XML 可扩展标记语言 (Extensible Markup Language), 标记 (markup) 是关键部分,是标准通用标记语言 (Standard Generalized Markup ...
随机推荐
- PDF阅读器关闭“使用手型工具阅读文章”功能
1.问题描述 某些PDF文件打开时,光标显示的手型工具里面有个箭头,一点击鼠标左键,就跳转到下一页了.给阅读带来很多不便. 2.原因 因为这类PDF文档中带有"文章"(articl ...
- 最新 大众书网java校招面经 (含整理过的面试题大全)
从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.大众书网等10家互联网公司的校招Offer,因为某些自身原因最终选择了大众书网.6.7月主要是做系统复习.项目复盘.Leet ...
- Java 8 集合之流式(Streams)操作, Streams API 详解
因为当时公司的业务需要对集合进行各种各样的业务逻辑操作,为了提高性能,就用到了这个东西,因为以往我们以前用集合都是需要去遍历(串行),所以效率和性能都不是特别的好,而Streams就可以使用并行的方式 ...
- 【剑指offer】面试题 24. 反转链表
面试题 24. 反转链表
- tcpdump网络调试
简介 用简单的话来定义tcpdump,就是:dump the traffic on a network,根据使用者的定义对网络上的数据包进行截获的包分析工具. tcpdump可以将网络中传送的数据包的 ...
- python之生成器yeild
python生成器Generator——yield 思考: 首先思考这样一个问题: 创建一个列表,但是内存受限,容量一定是有限的.那么如果创建了一个包含100万个元素的列表,不仅占用很大的存储空间,而 ...
- LOJ3119 CTS2019 随机立方体 概率、容斥、二项式反演
传送门 为了方便我们设\(N\)是\(N,M,L\)中的最小值,某一个位置\((x,y,z)\)所控制的位置为集合\(\{(a,b,c) \mid a = x \text{或} b = y \text ...
- Unity性能优化-音频设置
没想到Unity的音频会成为内存杀手,在实际的商业项目中,音频的优化必不可少. 1. Unity支持许多不同的音频格式,但最终它将它们全部转换为首选格式.音频压缩格式有PCM.ADPCM.Vorbis ...
- 通过设置启用 Visual Studio 默认关闭的大量强大的功能提升开发效率
原文:通过设置启用 Visual Studio 默认关闭的大量强大的功能提升开发效率 使用 Visual Studio 开发 C#/.NET 应用程序,以前有 ReSharper 来不足其各项功能短板 ...
- IdentityServer4:发布环境的数字签名证书
一,jwt的三个组成部件 先来看一个由IdentityServer颁发的一个标准令牌 eyJhbGciOiJSUzI1NiIsImtpZCI6IjBiNTE3ZjIzYWY0OGM4ZjkyZjExM ...