C#XML(四) - Linq to XML

Linq是C#3.0中出现的一个新特性,使用它可以方便的操作许多数据源,也包括XML文件.使用Linq操作XML文件非常的方便,而且也比较简单

1. 读取数据

直接找到元素为book的这个结点,然后遍历读取所有的结果.

                XElement xe = XElement.Load(@"..\..\example.xml");
                //elements为3个book节点组成的List(每个book节点对应一个List值)
                IEnumerable<XElement> elements = from ele in xe.Elements("book")
                                                 select ele;

2. 插入数据

插入结点和属性都采用new的方法

            XElement xe = XElement.Load(@"..\..\example.xml");
            //追加节点
            //  <book Type="选修课" ISBN="7-111-19149-1">
            //  <title> 计算机操作系统 </title>
            //  <author> 7 - 111 - 19149 - 1 </author>
            //  <price> 28 </price>
            //  </book>
            XElement record = new XElement(
            new XElement("book",
            new XAttribute("Type", "选修课"),
            new XAttribute("ISBN", "7-111-19149-1"),
            new XElement("title", "计算机操作系统"),
            new XElement("author", "7-111-19149-1"),
            new XElement("price", 28.00)));
            xe.Add(record);
            //保存文件
            xe.Save(@"..\..\example.xml");

3. 删除数据

首先得到选中的那一行,通过ISBN号来找到这个元素,然后用Remove方法直接删除

        //7-111-19149-1是对应着要删除的ISBN号
            string id = "7-111-19149-1";
            XElement xe = XElement.Load(@"..\..\example.xml");
            IEnumerable<XElement> elements = from ele in xe.Elements("book")
                                             where (string)ele.Attribute("ISBN") == id
                                             select ele;
            
                if (elements.Count() > 0)
                //找到第一个符合删除条件的节点进行删除
                elements.First().Remove();
            //删除节点后,保存更新后的文档
            xe.Save(@"..\..\Book.xml");

删除所有的数据

            XElement xe = XElement.Load(@"..\..\Book.xml");
            //选中所有的book节点
            IEnumerable<XElement> elements = from ele in xe.Elements("book")
                                             select ele;
            if (elements.Count() > 0)
            {
                //删除选中所有的book节点
                elements.Remove();
            }
            xe.Save(@"..\..\Book.xml");

4. 修改数据

首先得到所要修改的某一个结点,然后用SetAttributeValue来修改属性,用ReplaceNodes来修改结点元素

            //7-111-19149-1是对应着要查找的ISBN号
            string id = "7-111-19149-1";
            IEnumerable<XElement> element = from ele in xe.Elements("book")
                                                where ele.Attribute("ISBN").Value == id
                                                select ele;
                if (element.Count() > 0)
                {
                    XElement first = element.First();
                    ///设置新的属性
                    first.SetAttributeValue("Type", "类型");
                    ///替换新的节点
                    first.ReplaceNodes(
                            new XElement("title", "标题"),
                            new XElement("author", "作者"),
                            new XElement("price", "价格")
                            );
                
                xe.Save(@"..\..\Book.xml");

5. 隐藏空值

1. 给属性的特性添加IsNullable属性,并设置为false。当值为NULL时隐藏xml节点

public class MyClass
{
   [XmlElement(IsNullable = false)]
   public string Group;
}

2. 使用模式ShouldSerialize{PropertyName}创建一个函数,该函数告诉XmlSerializer是否应序列化该成员。

public class Person
{
  public string Name {get;set;}
  public int? Age {get;set;}
  public bool ShouldSerializeAge()
  {
    return Age.HasValue;
  }
}

3. 如果您将某些序列化为属性,则可以在您的类上具有一个名为{PropertyName}Specified的属性,以控制是否应该对其进行序列化。

public class MyClass
{
    [XmlAttribute]
    public int MyValue;
 
    [XmlIgnore]
    public bool MyValueSpecified;
}

LINQ to XML 编程基础 - Dawn.D - 博客园 (cnblogs.com)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值