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 ...
随机推荐
- k8s pv,pvc无法删除问题
一般删除步骤为:先删pod再删pvc最后删pv 但是遇到pv始终处于“Terminating”状态,而且delete不掉.如下图: 解决方法: 直接删除k8s中的记录: 1 kubectl patch ...
- HSRP 实验
一.环境准备 1. 软件:GNS3 2. 路由:c7200 二.实验操作 实验要求: 1.理解HSRP的工作原理. 2.掌握HSRP配置方法. 3.理解HSRP的抢占与跟踪作用. 实验拓扑: 实验过程 ...
- Prometheus入门到放弃(4)之cadvisor监控docker容器
Prometheus监控docker容器运行状态,我们用到cadvisor服务,cadvisor我们这里也采用docker方式直接运行. 1.下载镜像 [root@prometheus-server ...
- Servlet知识点小结
Servlet: server applet * 概念:运行在服务器端的小程序 * Servlet就是一个接口,定义了Java类被浏览器访问到(tomcat识别)的规则. * 将来我们自定义一个类,实 ...
- Java开发笔记(一百二十二)AWT选择框
前面介绍了两种文本输入框的用法,不过实际应用很少需要用户亲自文字,而是在界面上列出几个选项,让用户勾勾点点完成选择,这样既方便也不容易弄错.依据选择的唯一性,可将选项控件分为两类:一类是在方框中打勾的 ...
- 2019秋季PAT甲级_C++题解
2019 秋季 PAT (Advanced Level) C++题解 考试拿到了满分但受考场状态和知识水平所限可能方法不够简洁,此处保留记录,仍需多加学习.备考总结(笔记目录)在这里 7-1 Fore ...
- 【HC89S003F4开发板】 10汇编指令
HC89S003F4开发板汇编指令 一.数据传递类指令 MOV.MOVC.MOVX 1.MOV,用于片内数据存储器中的数据传递指令中. 2.MOVC是与ROM之间的数据传送,而MOVX是与外部RAM数 ...
- Linux+QT界面开发(含数据库)小结
今天十一国庆节,过了一个很有意义的国庆节,去了龙岗区图书馆,第一次知道了借的书可以在任意分馆归还!这个切实方便了广大读者.针对Linux应用,特意借阅一本:<linux环境下Qt4图形界面与My ...
- @Autowired 和 @Resource注解, 一个接口有多个实现类的时候Spring注入遇到的问题
先说下我遇到的问题,有一个接口 CompensationService, 有两个实现类 MusicCompensationStrategyImpl 和 TakeDeliveryCompensati ...
- mysql中常见正则表达式的应用
查找name字段中以'st'为开头的所有数据: mysql> SELECT name FROM person_tbl WHERE name REGEXP '^st'; 查找name字段中以'ok ...