LINQ系列:LINQ to XML类

 LINQ to XML由System.Xml.Linq namespace实现,该namespace包含处理XML时用到的所有类。在使用LINQ to XML时需要添加System.Xml.Linq.dll的引用,在代码声明中添加using:

using System.Xml.Linq;

1. System.Xml.Linq namespace的类及其描述

描述
XAttribute XML中的一个属性
XCData CDATA文本节点
XComment XML注释
XContainer 一个抽象基类,表示具有子节点的节点
XDeclaration XML声明
XDocument XML文档,该类派生自XContainer类
XDocumentType XML DTD(文档类型定义)
XElement XML元素,是XContainer的派生类
XName XML元素或属性的名称
XNamespace XML命名空间
XNode XML元素树的节点
XNodeDocumentOrderComparer 根据节点在XML文档内的顺序对它们进行比较
XNodeEqualityComparer 根据节点的值对它们进行比较
XObject 表示XNodes和XAttributes的抽象类
XObjectChange XObject事件发生时的事件类型
XObjectChangeEventArgs 为Changing和Chenged事件提供信息和数据
XObjectChangeEventHandler 处理XObject中Changed和Changing事件的方法
XProcessingInstruction XML处理指令
XText XML文本

  示例:XML声明指定XML版本、XML文档的编码,以及XML文档是否是一个独立的文件。

复制代码
// using System.Xml.Linq;
XDocument doc = new XDocument
(
    new XDeclaration("1.0", "utf-8", "yes"),
    new XElement("Root", "LINQ to XML")
);
string xml = doc.Declaration.ToString() + Environment.NewLine + doc.ToString();
Console.WriteLine(xml);
复制代码

  运行结果:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>LINQ to XML</Root>

2. XElement类

  XElement类表示XML元素,它是XContainer类的派生类,而XContainer类又派生于XNode类。一个元素就是一个节点,XElement是LINQ to XML最重要最基本的类之一,它包含所有创建和操作XML元素所必需的功能。通过它可以创建元素,添加和修改元素的属性,操作元素的内容等。

复制代码
XDocument doc = new XDocument
(
    new XDeclaration("1.0", "utf-8", "yes"),
    new XComment("Created by LINQ to XML"),
    new XElement
    (
        "Products",
        new XElement("ProductID", 1),
        new XElement("ProductName", "LINQ to XML"),
        new XElement("UnitPrice", 10m)
    )
);
复制代码
复制代码
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<!--Created by LINQ to XML-->
<Products>
  <ProductID>1</ProductID>
  <ProductName>LINQ to XML</ProductName>
  <UnitPrice>10</UnitPrice>
</Products>
复制代码

3. XAttribute类

  XAttribute类用来处理属性,属性是与元素相关联的名称/值对。

复制代码
XElement product = new XElement
(
    "Root",
    new XElement
    (
        "Product",
        new XAttribute("id", 1)
    )
);
复制代码
<Root>
  <Product id="1" />
</Root>
复制代码
XElement product = new XElement
(
    "Root",
    new XElement
    (
        "Product",
        new XAttribute("ProductID", 1),
        new XAttribute("ProductName", "LINQ to XML")
    )
);
复制代码
<Root>
  <Product ProductID="1" ProductName="LINQ to XML" />
</Root>

  删除元素属性:

XAttribute attr = product.Element("Product").Attribute("ProductName");
attr.Remove();

4. XDocument类

  XDocument类提供了处理有效XML文档的方法,包括声明、注释和处理指令。XDocument类派生自XContainer类,可以有子节点。XML标准限值XDocument对象只包含单个XElement子节点,此节点作为根节点或跟元素。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苍狼_2001

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值