使用Linq对XML进行增删查改

1. 引入 命名空间

using System.Xml.Linq;

2. Xml的结构


<?xml version="1.0" encoding="utf-8"?>
<Products>
  <Product Id="1">
    <Name>HTC One</Name>
    <Price>4856.23</Price>
    <Count>100</Count>
  </Product>
  <Product Id="2">
    <Name>Iphone 6 Update</Name>
    <Price>5856.23</Price>
    <Count>655</Count>
  </Product>
  <Product Id="3">
    <Name>Lenno K800</Name>
    <Price>3856.23</Price>
    <Count>898</Count>
  </Product>
  <Product Id="4">
    <Name>HuaWei P7</Name>
    <Price>2856.23</Price>
    <Count>1000</Count>
  </Product>
  <Product Id="5">
    <Name>MI 4</Name>
    <Price>1856.23</Price>
    <Count>1</Count>
  </Product>
</Products>

3. 后台程序


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

using System.Xml.Linq;

namespace XMLLinq
{
    class Program
    {
        static void Main(string[] args)
        {
            string Path = AppDomain.CurrentDomain.BaseDirectory;
            string xmlPath = System.IO.Path.Combine(Path, "../../Product.xml");

           XElement xmlDoc = XElement.Load(xmlPath);


           //1.取所有节点的数据

           IEnumerable<XElement> xmlCollection = xmlDoc.Elements("Product");

           Console.WriteLine("Count:"+xmlCollection.Count());

           var linq = from x in xmlCollection select new {Name=x.Element("Name").Value,Price = x.Element("Price").Value,Count=x.Element("Count").Value};

            foreach(var e in linq){
            Console.WriteLine("Name={0},Price={1},Count={2}",e.Name,e.Price,e.Count);
            }


            //2. 取某一个节点
            Console.WriteLine("One Record:");
            var oneObj = xmlCollection.Where(x => x.Attribute("Id").Value.Equals("2")).First();
            Console.WriteLine("Name={0},Price={1},Count={2}", oneObj.Element("Name").Value, oneObj.Element("Price").Value, oneObj.Element("Count").Value);
          

            //3. 新增一个节点
            XElement newElement = new XElement(
                "Product",
                new XAttribute("Id", xmlCollection.Count()+1),
                new XElement("Name","new Product"),
                new XElement("Price",new decimal(100)),
                 new XElement("Count",200)
                );

            xmlDoc.Add(newElement);
            xmlDoc.Save(xmlPath);

            //修改节点的值
            Console.WriteLine("Upldate Record:");
            var UpdateObj = xmlCollection.Where(x => x.Attribute("Id").Value.Equals("2")).First();
            UpdateObj.SetElementValue("Name", "Iphone 6 Update");
            xmlDoc.Save(xmlPath);

            //删除一个节点

            Console.WriteLine("Delete Record:");
            var deleteObj = xmlCollection.Where(x => x.Attribute("Id").Value.Equals("6")).FirstOrDefault();
            if(null != deleteObj){
             deleteObj.Remove();
             xmlDoc.Save(xmlPath);
            }
           
          


            Console.ReadLine();
        }
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

sust2012

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

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

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

打赏作者

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

抵扣说明:

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

余额充值