zoukankan      html  css  js  c++  java
  • LINQ to XML 建立,读取,增,删,改

     

    LINQ to XML的出现使得我们再也不需要使用XMLDocument这样复杂的一个个的没有层次感的添加和删除。LINQ可以使的生成的XML文档在内存中错落有致。下面以一个小的例子说名LINQ to XML的简单应用。

    • 需要添加必要的引用。System.XML.Linq , System.XML.Xpath
    • 使用XDocument 建立一个XML文档。XDeclaration 声明开头字样。XComment 添加相应的注释。而XElement则是NODE的名字和内容。
    • 把一个XDocument 存储起来使用Save方法,而读取是Load,参数可以指定所要的路径。
    • 增加 使用Add()方法,新建节点。或者使用LINQ语句配合使用。
    • 读取,可以通过XElement对象的Descendant()的名字,读取相应的node,Value属性是node对应的值,而Name是node的名字。
    Code
    var userName = from un in xe.Descendants("Name")
    select un.Value;
    • 修改可以 使用2种方法。
    1. 其一是直接对Value和Name进行数值的修改。
    2. 使用Replace相关的方法等等。比如
    Code
    var a = age.Single<XElement>();
    a.ReplaceWith(
    new XElement("agettt", "26"));
    //读取唯一一个,进行名字和数字的修改。
    • 删除一个或者多个node,或者单独删除里面的值而不删除节点名字。可以有多种方法,Remove ,Removeall,RemoveNodes等等方法。

    下面是一个完整的控制台程序,以演示简单的增删改功能的实现。是不是比XMLDocument来的简单得多呢,大家赶快试试吧:)

    Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Xml.Linq;
    using System.Xml.XPath;

    namespace LINQTOXMLDEMO
    {
    class Program
    {
    staticvoid Main(string[] args)
    {
    XDocument userInfomation
    =new XDocument(
    new XDeclaration("1.0", "utf-8", "yes"),
    new XComment("This is a comment.Just input what you want to say."),
    new XElement("UsersInfomation",
    new XElement("User", new XAttribute("ID", "1"),
    new XElement("Name", "Alex"),
    new XElement("Hometown", "HRB"),
    new XElement("Age", "22")),
    new XElement("User",
    new XAttribute("ID", "2"),
    new XElement("Name", "Fu"),
    new XElement("Hometown", "SY"),
    new XElement("Age", "27")),
    new XElement("User",new XAttribute("ID","3"),
    new XElement("Name","Abe"),
    new XElement("Hometown","HRB"),
    new XElement("Age","24"))
    ));

    // 显示所生成的XML在内存中的信息。并且按着所制定的路径保存。
    Console.WriteLine(userInfomation);
    userInfomation.Save(
    @"D:1userInfomation.xml");

    XElement xd
    = XElement.Load(@"D:1userInfomation.xml");
    GetAllUsername(xd);
    Console.WriteLine();
    GetAge(xd);
    Console.WriteLine();
    GetHometown(xd);
    Console.WriteLine();
    updateAge(xd);
    //Console.WriteLine(xd);
    deleteElement(xd);
    Console.WriteLine();
    addNode(xd);
    Console.WriteLine(xd);
    Console.ReadKey();
    }
    //得到所有name字段的名字
    privatestaticvoid GetAllUsername(XElement xe)
    {
    var userName
    = from un in xe.Descendants("Name")
    select un.Value;
    foreach (var name in userName)
    {
    Console.WriteLine(
    "name {0}" ,name);
    }
    }
    //输出AGE是24的所有NODE
    privatestaticvoid GetAge(XElement xe)
    {
    var age
    = from c in xe.Descendants("Age")
    where c.Value =="24"
    select c;
    foreach (var a in age)
    Console.WriteLine(
    "AGE: {0}", a);
    }
    //移除所有的Name字段
    privatestaticvoid deleteElement(XElement xe)
    {
    var user
    = from u in xe.Descendants("User")
    select u;
    foreach (var u in user)
    {

    var names
    = from name in u.Descendants("Name")
    select name;
    names.Remove();
    }
    }
    //增加节点
    privatestaticvoid addNode(XElement xe)
    {
    var user
    = from u in xe.Descendants("User")
    select u;
    foreach (var u in user)
    {
    u.Add(
    new XElement("Nation","China"));
    }
    }
    //更新字段NODE的名字和内容。
    privatestaticvoid updateAge(XElement xe)
    {
    var age
    = from c in xe.Descendants("Age")
    where c.Value =="24"
    select c;
    var a
    = age.Single<XElement>();
    a.ReplaceWith(
    new XElement("agettt", "26"));
    }
    //统计hometown为HRB的人数
    privatestaticvoid GetHometown(XElement xe)
    {
    var hometown
    = from h in xe.Descendants("Hometown")
    where h.Value =="HRB"
    select h;
    int count=0;
    foreach (var h in hometown)
    {
    count
    ++;
    }
    Console.WriteLine(count);
    }
    }
    }

    几个例子的作用,代码函数都有注释。比较简单。不需要太多的介绍。输出结果如下图所示。

     
     
     
  • 相关阅读:
    使用 OpenSmtp.dll 发送邮件 (记录) 西安
    国庆假期加班头疼 西安
    asp.net 下 使用 showModalDialog 模式窗口 (记录) 西安
    严重声讨 西安
    牙痛,医生说我这是根尖周炎,有点郁闷
    Google域名被国内某商抢注 竟只得重金去赎
    Windows自带的一个罕为人知的无敌命令
    在CSS中使用继承
    删除字符串最后一个字符的几种方法
    如何在一个RowFilter过的dataview中增加一行
  • 原文地址:https://www.cnblogs.com/xiaofengfeng/p/3509185.html
Copyright © 2011-2022 走看看