目录

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. 密码工具:KeePassXC

    KeePassXC 开源免费 只支持数据库存放在本地, 跨平台 自动填充 使用浏览器插件时,软件必须启动着 KeePassXC is a community fork of KeePassX, a n ...

  2. memcached概述与基本操作

    memcached 什么是memcached memcached之前是danga的一个项目,最早是为LiveJournal服务的,当初设计师为了加速LiveJournal访问速度而开发的,后来被很多大 ...

  3. webpack 4.0改版问题

    4.0之后的打包方式: webpack --mode development src/index.js --output-filename app.js --output-path dist

  4. h5上拉加载更多

    --------------------------------------------------------------------------例子1 <div class="bo ...

  5. LeetCode 563. 二叉树的坡度(Binary Tree Tilt) 38

    563. 二叉树的坡度 563. Binary Tree Tilt 题目描述 给定一个二叉树,计算整个树的坡度. 一个树的节点的坡度定义即为,该节点左子树的结点之和和右子树结点之和的差的绝对值.空结点 ...

  6. Java基础笔试练习(九)

    1.下面所示的java代码,运行时,会产生()类型的异常 ? int Arry_a[] = new int[10]; System.out.println(Arry_a[10]); A.Arithme ...

  7. MySQL必知必会2

    使用数据处理函数 函数 与其他大多数计算机语言一样,SQL支持利用函数来处理数据.函数一般是在数据上执行的,他给数据的转换和处理提供了方便,在前一章中用来去掉尾空格的RTrim()就是一个函数的例子 ...

  8. 使用ImagesPipeline时候报错为:ModuleNotFoundError: No module named 'scrapy.contrib'

    刚开始我是这样写的: 报错为: 哈哈,经过查阅资料,其实他是存在的,接下来修改如下: 经过运行结果如下: 问题解决,哈哈哈,搞定!!!!!!!!!!!!

  9. Django content-type组件

    介绍 Django包含一个contenttypes应用程序(app),可以跟踪Django项目中安装的所有模型(Model),提供用于处理模型的高级通用接口. Contenttypes应用的核心是Co ...

  10. PMM--简介与部署

    一.PMM架构 Percona Monitoring and Management是percona一款开源的用于管理和监控MySQL 和MongoDB性能的开源平台,通过PMM客户端收集到的DB监控数 ...