目录

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属性节点

最新文章

  1. nginx源码分析之hash的实现
  2. php 不用四舍五入的方式截取小数点后两位
  3. 个人对于Virtual DOM的一些理解
  4. XAF学习笔记之 Upcasting
  5. CSS3技巧:利用css3径向渐变做一张优惠券(转)
  6. CentOS 6.4 中安装部署 Nutch 1.7
  7. jdbc mysql 取数,突然取不到数据,数据库中有数据
  8. php Magic methods __call
  9. Verilog HDL模块的结构
  10. Reverse Interger
  11. Hi3531支持2GByte内存
  12. 【NOIP2013TG】solution
  13. Linux中find命令的用法汇总
  14. 【Linux】基本命令操作
  15. 用dtree实现树形菜单 dtree使用说明
  16. Linux系统之路——用CentOS 7打造合适的科研环境
  17. (转)基于keepalived搭建MySQL的高可用集群
  18. 浅谈蓝牙低功耗(BLE)的几种常见的应用场景及架构(转载)
  19. java限流(一): Semaphore
  20. KMP匹配

热门文章

  1. 【Python学习之十】操作数据库
  2. Altera FPGA 远程升级有关的几个IP的使用
  3. Clean code 关于注释、函数、命名的感想
  4. 【转帖】处理器史话 | 服务器CPU市场的战役, AMD、Intel和ARM的厮杀
  5. Hbase面试题
  6. Zabbix案例实践|Zabbix屏蔽告警
  7. flask框架(八)—自定义命令flask-script、多app应用、wtforms表单验证、SQLAIchemy
  8. AspNetCore Redis实现分布式缓存
  9. [UOJ #167]【UR #11】元旦老人与汉诺塔
  10. Python 3 MySQL数据库操作