C#沉淀-Linq to XML实战

XML类Linq to XML可以以两种方式和XML配合。第一种方式是作为简化的XML操作API,和二种方式是使用本章前面看到的Linq查询工具Linq to XML API由很多表示XML树组件的类组成,其中有三个类比较重要:XElement/XAttribute/XDocument从示例出发:using System;using System.Collections.Generic...
摘要由CSDN通过智能技术生成

XML类

Linq to XML可以以两种方式和XML配合。第一种方式是作为简化的XML操作API,和二种方式是使用本章前面看到的Linq查询工具

Linq to XML API由很多表示XML树组件的类组成,其中有三个类比较重要:XElement/XAttribute/XDocument

从示例出发:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Xml.Linq;

namespace CodeForLinq
{
   
    class Program
    {
   
        static void Main(string[] args)
        {
   
            //XDocument表示一个XML文档
            //XElement表示一个XML元素
            XDocument employees1 =
                new XDocument(
                    //创建一个Employees根节点
                    new XElement("Employees",
                        //创建两个子节点
                        new XElement("Name", "Bob Smith"),
                        new XElement("Name", "Sally Jones") 
                        )
                    );

            //通过Save方法将XML文档保存
            employees1.Save("Employees.xml");

            //通过Load方法加载XML文档
            XDocument employees2 = XDocument.Load("Employees.xml");

            //打印出XML文档结构
            Console.WriteLine(employees2.ToString());

            Console.ReadKey();
        }
    }
}

输出:

<Employees>
  <Name>Bob Smith</Name>
  <Name>Sally Jones</Name>
</Employees>
XML树的嵌套关系
  • XDocument (文档)
    • XDeclaration ?
    • XDocumentType ?
    • XProcessingInstruction *
    • XElement ? (根节点)
      • XElement * (节点)
      • XComment *
      • XAttribute *
      • XProcessingInstruction *

?表示0个或1个;*表示这个或多个

语法解析:

public XDocument(params object[] content);

参数表示文档所包含的内容,即要添加到此文档的内容对象的参数列表,上例添加了一个根节点

public XElement(XName name, params object[] content);

第一个参数是对象名

第二个参数以及之后的参数包含了XML树的节点;第二个参数是一个Params参数,也就是说可以有任意多个参数

然后在根点中又添加了两个节点

使用XML树的值

用于获取XML树的值的方法有:

  • Nodes-所在类为XDocumet/XElement,返回类型是IEnumerable<object>,它会返回当前节点的所有子节点,不管什么什么类型,可以使用OfType来指定返回某个类型的节点,如IEnmerable<XCmment> comments = xd.Nodes().OfType<XComment>();,返回XComment类型的节点
  • Elements-所在类为XDocumet/XElement,返回类型是IEnumerable<XElement>,它返回当前节点的XElement子节点,或所有具有某个 名字的子节点
  • Element-所在类为XDocumet/XElement,返回类型是Element,它返回当前一个的Element子节点,或所有具有某个 名字的子节点
  • Descendants-所在类为XElement,返回类型是IEnumerable<XElement>,它返回所有的XElement子代节点,或所有具有某个名字的XElement子代节点,不管它们处于当前节点下嵌套的什么层次
  • DescendantsAndSelf-所在类为XElement,返回类型是IEnumerable<XElement>,和Descendants一样,但是包括当前节点
  • Ancestors-所在类为XElement,返回类型是IEnumerable<XElement>,返回所有上级XElement节点,或者所有具有某个名字的上级XElement节点
  • AncestorsAndSelf-所在类为XElement,返回类型是IEnumerable<XElement>,和AncestorsAndSelf一样,但是包括当前节点
  • Parent-所在类为XElement,返回类型是XElement,返回当前节点的父节点

示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Xml.Linq;

namespace CodeForLinq
{
   
    class Program
    {
   
        static void Main(string[] args)
        {
   

            //XDocument表示一个XML文档
            //XElement表示一个XML元素
            XDocument employees1 =
                new XDocument(

                //创建一个Employees根节点
                    new XElement("Employees",

                //创建两个子节点
                        new XElement("Employee",
                            new XElement("Name", "Bob Smith"),
                            new XElement("PhoneNumber", "13161861814")),
                        new XElement("Employee",
                            new XElement("Name", "Sally Jones"),
                            new XElement("PhoneNumber", "13161861815"),
                            new XElement("PhoneNumber", "13161861816"))

                            ));

            //获取第一个名为"Employees"的子XElement
            XElement root = employees1.Element("Employees");//其实就是根节点
            IEnumerable<XElement> employess = root.Elements();//根节点下所有的子节点

            foreach (XElement emp in employess)
            {
   
                //获取第一个名为"Name"的子XElement
                XElement empNameNode 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值