zoukankan      html  css  js  c++  java
  • 黄聪:Linq初级班 Linq To XML体验(编程篇)

    Linq初级班 Linq To XML体验(编程)

    首先,我们在Linq初级班 Linq To XML体验(基础)已经初步了解了一些XML的基本知识,在本章我们将试着用LINQ to XML对XML文档进行操作,我也是LINQ的初学者,文章中不免会有一些不对的地方,希望高手们多多指点,为我们LINQ初学者们多提宝贵的意见,我也会继续努力的,在本章我们将学习如何用LINQ to XML创建,加载,操作以及遍历XML文档,文章目录如下所示:

    目录

    1.使用Linq to XML加载XML文档

    2.使用Linq to XML创建保存XML

    3.使用Linq to XML遍历XML

    4.使用Linq to XML操作XML的元素

    5.使用Linq to XML操作XML的属性

    1.使用Linq to XML加载XML文档

    首先在工程下创建好一个XML文档,命名为test.xml,内容如下:

    代码
    <?xml version="1.0" encoding="utf-8"?>
    <?xml-stylesheet title='黄聪'?>
    <Root>
    <Persons>
    <Person Id="1">
    <Name>Huang Cong</Name>
    <Sex></Sex>
    </Person>
    </Persons>
    </Root>

    测试程序:

    代码
    //-----------------------------------------------------------
    // All Rights Reserved , Copyright (C) 2010 ,黄聪 , Ltd .
    //-----------------------------------------------------------

    using System;
    using System.Xml.Linq;
    using System.IO;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    //从xml文件中加载
    XElement xel1 = XElement.Load("test.xml");

    Console.WriteLine(
    "从xml文件中加载:");
    Console.WriteLine(xel1);
    Console.WriteLine(
    "---------------------------------");

    //从字符串中加载
    XElement xel2 = XElement.Parse(@"
    <Root>
    <Persons>
    <Person Id='1'>
    <Name>Huang Cong</Name>
    <Sex>男</Sex>
    </Person>
    </Persons>
    </Root>
    ");
    Console.WriteLine(
    "从字符串中加载:");
    Console.WriteLine(xel2);
    Console.WriteLine(
    "---------------------------------");

    //从TextReader中加载
    TextReader tr = new StringReader(@"
    <Root>
    <Persons>
    <Person Id='1'>
    <Name>Huang Cong</Name>
    <Sex>男</Sex>
    </Person>
    </Persons>
    </Root>
    ");
    XElement xel3
    = XElement.Load(tr);
    Console.WriteLine(
    "从TextReader中加载:");
    Console.WriteLine(xel3);
    Console.WriteLine(
    "---------------------------------");
    }
    }
    }

    运行结果:

    2.使用Linq to XML创建保存XML

    Linq to XML提供了一组简单而强大的方法来手工创建XML元素,在Linq初级班 Linq To XML体验(基础)中也有所演示了,需要注意的是,Linq to XML提供了自动缩进功能,这使得可读性很强,下面就演示如何用单个语句来创建一个XML树.此外使用Linq to XML保存XML和加载XML一样简单,使用Save()方法可以将XML写入一个test.xml文件中.这种保存XML的方法通常也叫做序列化.

    代码
    //-----------------------------------------------------------
    // All Rights Reserved , Copyright (C) 2010 ,黄聪 , Ltd .
    //-----------------------------------------------------------

    using System;
    using System.Xml.Linq;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    //保存
    XElement xel1 = XElement.Parse(@"
    <Root>
    <Persons>
    <Person Id='1'>
    <Name>Huang Cong</Name>
    <Sex>男</Sex>
    </Person>
    </Persons>
    </Root>
    ");
    //保存至test1.xml文档中
    xel1.Save("test1.xml");

    XElement xel2
    = new XElement(
    new XElement("Root",
    new XElement("Child1", 1),
    new XElement("Child2", 2))
    );

    //保存至test2.xml文档中
    xel2.Save("test2.xml");
    }
    }
    }

    运行结果:

    3.使用Linq to XML遍历XML

    代码
    //-----------------------------------------------------------
    // All Rights Reserved , Copyright (C) 2010 ,黄聪 , Ltd .
    //-----------------------------------------------------------

    using System;
    using System.Xml.Linq;
    using System.Linq;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    //保存
    XElement xel = XElement.Parse(@"
    <Root>
    <Persons>
    <Person>Huang Cong</Person>
    <Person>Zhang San</Person>
    <Person>Li Si</Person>
    <Person>Wang Wu</Person>
    </Persons>
    </Root>
    ");
    Console.WriteLine(
    "使用XNode遍历:");
    foreach (XNode n in xel.Nodes())
    {
    Console.WriteLine(n);
    }
    Console.WriteLine(
    "----------------------------");
    Console.WriteLine(
    "使用XElement遍历:");
    foreach (XElement e in xel.Elements("Persons"))
    {
    Console.WriteLine(e);
    }
    Console.WriteLine(
    "----------------------------");
    Console.WriteLine(
    "使用Lambda表达式遍历:");
    var query
    = xel.Elements().Select(u => u);
    foreach (var q in query)
    {
    Console.WriteLine(q);
    }
    }
    }
    }

    运行结果:

    4.使用Linq to XML操作XML的元素

    Linq to XML可以方便地修改XML树,如添加,删除,更新和复制XML文档的内容其中包括:
    1.插入:使用XNode类中的某一种添加方法可以很容易地向一个XML树添加内容.(AddAfterSelf,AddBeforeSelf)
    2.更新:使用LINQ to XML更新XML相当简单.(ReplaceWith)
    3.删除:使用LINQ to XML删除XML只要导航到要删除的内容调用删除方法即可.(Remove,RemoveAll)

    (1)插入元素

    代码
    //-----------------------------------------------------------
    // All Rights Reserved , Copyright (C) 2010 ,黄聪 , Ltd .
    //-----------------------------------------------------------

    using System;
    using System.Xml.Linq;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    //保存
    XElement xel = new XElement(new XElement("Root",
    new XElement("Person",
    new XElement("Name","Huang Cong"))));

    //在Hang Cong之前插入一个元素
    xel.Element("Person").Element("Name").AddBeforeSelf(new XElement("Sex", ""));

    //在Hang Cong之后插入一个元素
    xel.Element("Person").Element("Name").AddAfterSelf(new XElement("Age", 22));

    Console.WriteLine(xel);
    }
    }
    }

    运行结果:

    (2)更新元素

    代码
    //-----------------------------------------------------------
    // All Rights Reserved , Copyright (C) 2010 ,黄聪 , Ltd .
    //-----------------------------------------------------------

    using System;
    using System.Xml.Linq;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    //保存
    XElement xel = new XElement(new XElement("Root",
    new XElement("Person",
    new XElement("Name", "Huang Cong"),
    new XElement("Sex", ""),
    new XElement("Age", 22))));

    //更新某个元素
    xel.Element("Person").Element("Name").ReplaceWith(new XElement("Name","Li Si"));
    Console.WriteLine(xel);

    Console.WriteLine(
    "--------------------------");

    //更新一个元素以及子节点
    xel.Element("Person").ReplaceWith(new XElement("Person",
    new XElement("Name", "Zhang San"),
    new XElement("Sex", ""),
    new XElement("Age", 23)));
    Console.WriteLine(xel);
    }
    }
    }

    运行结果:

    (3)删除元素

    代码
    //-----------------------------------------------------------
    // All Rights Reserved , Copyright (C) 2010 ,黄聪 , Ltd .
    //-----------------------------------------------------------

    using System;
    using System.Xml.Linq;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    //保存
    XElement xel = new XElement(new XElement("Root",
    new XElement("Person",
    new XElement("Name", "Huang Cong"),
    new XElement("Sex", ""),
    new XElement("Age", 22))));

    //从父节点删除该元素,包括该元素下的所有子节点
    xel.Element("Person").Element("Sex").Remove();
    Console.WriteLine(xel);

    Console.WriteLine(
    "--------------------------");

    //删除某个元素下的所有子节点
    xel.Element("Person").RemoveAll();

    Console.WriteLine(xel);
    }
    }
    }

    运行结果:

    5.使用Linq to XML操作XML的属性

    代码
    //-----------------------------------------------------------
    // All Rights Reserved , Copyright (C) 2010 ,黄聪 , Ltd .
    //-----------------------------------------------------------

    using System;
    using System.Xml.Linq;
    using System.Linq;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    //创建XML树的时候添加几个属性
    XElement xel = new XElement(new XElement("Root",
    new XElement("Person",
    new XAttribute("Name", "Huang Cong"),
    new XAttribute("Sex", ""),
    new XAttribute("Age", 22))));
    Console.WriteLine(xel);
    Console.WriteLine(
    "--------------------------");

    Console.WriteLine(
    "遍历某个元素的所有属性:");
    foreach (XAttribute att in xel.Element("Person").Attributes())
    {
    Console.WriteLine(att);
    }
    Console.WriteLine(
    "--------------------------");

    Console.WriteLine(
    "使用LINQ遍历某个元素的属性:");
    var query
    = xel.Element("Person").Attributes().Where(a => a.Name == "Name" || a.Name == "Sex").Select(a => a);
    foreach (var q in query)
    {
    Console.WriteLine(q);
    }
    Console.WriteLine(
    "--------------------------");

    Console.WriteLine(
    "获取某个元素第一个属性:");
    Console.WriteLine(xel.Element(
    "Person").FirstAttribute.Value.ToString());

    Console.WriteLine(
    "获取某个元素最后一个属性:");
    Console.WriteLine(xel.Element(
    "Person").LastAttribute.Value.ToString());
    Console.WriteLine(
    "--------------------------");

    Console.WriteLine(
    "使用Remove方法删除某个属性");
    xel.Element(
    "Person").Attribute("Sex").Remove();
    Console.WriteLine(xel);
    Console.WriteLine(
    "--------------------------");

    Console.WriteLine(
    "使用SetAttributeValue方法删除某个属性");
    xel.Element(
    "Person").SetAttributeValue("Age", null);
    Console.WriteLine(xel);
    Console.WriteLine(
    "--------------------------");
    }
    }
    }

    运行结果:

    小结:

    LINQ to XML 提供使用 .NET 语言集成查询 (LINQ) Framework 的内存中 XML 编程接口。相当于更新的和重新设计的文档对象模型 (DOM) XML 编程接口。本文用许多示例讲解了LINQ to XML如何在实际中应用,但是因为本人也是初学,肯定有不足之处,还望高手们多多指点~~

    相关文章

    1.LinQ初体验 简单的示例(原创)

    2.Linq初体验 Linq2SQL示例(原创)

    3.Linq推迟查询的执行(原创)

    4.Ling初体验 匿名类型在查询表达式中的使用

    5.Linq初级班 标准查询操作符的使用和调用

    6.Linq初级班 Linq To XML体验(编程篇)

    7.Linq初级班 Linq To XML体验(基础篇)

    8.Linq初级班 Linq To XML体验(高级编程篇)

  • 相关阅读:
    java.lang.NoClassDefFoundError:org/apache/commons/lang/exception/NestableRuntimeException错误的解决
    json转换成对象
    16年4月20号 个人总结
    sql between and
    sql 中的运算符级别 如and or not
    mybatis int 类型判断<if>
    iOS地址编码解析
    iosiOS 地图 自定义以及添加锚点
    ios如何获取位置权限
    如何初始化一个iOS原生地图
  • 原文地址:https://www.cnblogs.com/huangcong/p/1922326.html
Copyright © 2011-2022 走看看