.NET XML增删查改

 


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

namespace LinqDBPro.App_Code
{
    class XMLOp
    {

        //xml的读写更新,删除

        //1. 生成xml
        public void createXml() {
            XmlDocument document = new XmlDocument();
            XmlElement root = document.CreateElement("Customers");
            document.AppendChild(root);
            Console.WriteLine("开始生成xml");
            for (int i = 0; i < 10;i++ )
            {
                XmlElement childNode = document.CreateElement("Customer");
                childNode.SetAttribute("customerId", i.ToString());
                childNode.SetAttribute("customerCode",Guid.NewGuid().ToString());

                XmlElement customerNode = document.CreateElement("customerName");
                customerNode.InnerText = "company("+(i+1)+")";

                XmlElement customerAddressNode = document.CreateElement("customerAddress");
                customerAddressNode.InnerText = "深圳"+i;

                childNode.AppendChild(customerNode);
                childNode.AppendChild(customerAddressNode);

                root.AppendChild(childNode);
            }

            document.Save("d:\\data\\customer.xml");
            Console.WriteLine("生成完毕..");
        }

        //解析Xml

        public void readXml() {
            XmlDocument document = new XmlDocument();
            document.Load("d:\\data\\customer.xml");

            XmlNode root = document.SelectSingleNode("Customers");  //取到根节点
            XmlNodeList childNodeList =  root.ChildNodes;         //取到根节点下的所有子节点
            foreach( XmlNode childNode in childNodeList ){
                XmlElement childElement = childNode as XmlElement;
                string customerId = childElement.GetAttribute("customerId");
                string customerCode = childElement.GetAttribute("customerCode");
                Console.WriteLine(customerId+":"+customerCode);
                //取customer下的子节点
                XmlNodeList cNodeList = childElement.ChildNodes;
                foreach(XmlNode node in cNodeList ){
                    XmlElement cElement = node as XmlElement;
                    Console.WriteLine(cElement.Name + ":" + cElement.InnerText);
                }
            }

        }


        //更新节点,eg:给每个子节点加一个customerEnglishName
        public void updateXml() {
            string xmlPath = "d:\\data\\customer.xml";
            XmlDocument document = new XmlDocument();
            Console.WriteLine("bgin to read xml..");
            document.Load(xmlPath);
            Console.WriteLine("read xml ok..\n get the root node.");
            XmlNode root = document.SelectSingleNode("Customers");
            XmlNodeList childList = root.ChildNodes;
            foreach (XmlNode node in childList)
            {
              
                XmlElement childElement = node as XmlElement;

                Console.WriteLine("update node :"+childElement.GetAttribute("customerId"));

                XmlElement customerEnglishNameNode = document.CreateElement("customerEnglishName");
                customerEnglishNameNode.InnerText = "HuaWei co.ltd";

                childElement.AppendChild(customerEnglishNameNode);

            }
            document.Save(xmlPath);
        }

        //增加一个新的节点
        public void addNewNode(){
            string xmlPath = "d:\\data\\customer.xml";
            XmlDocument document = new XmlDocument();
            document.Load(xmlPath);

            XmlNode root = document.SelectSingleNode("Customers");
            Console.WriteLine("begin add..");
            XmlElement newElement = document.CreateElement("Customer");
            newElement.SetAttribute("customerId","10");
            newElement.SetAttribute("customerCode", Guid.NewGuid().ToString());

            XmlElement newCustomerNameNode = document.CreateElement("customerName");
            newCustomerNameNode.InnerText = "newCompany";

            XmlElement newCustomerAddressNode = document.CreateElement("customerAddress");
            newCustomerAddressNode.InnerText = "xi'an";

            XmlElement newCustomerEnglishNode = document.CreateElement("customerEnglishName");
            newCustomerEnglishNode.InnerText = "new inc.";
   
            newElement.AppendChild(newCustomerNameNode);
            newElement.AppendChild(newCustomerAddressNode);
            newElement.AppendChild(newCustomerEnglishNode);

            root.AppendChild(newElement);

            document.Save(xmlPath);

            Console.WriteLine("add ok..");

        }


        //删除某个节点
        public void deleteNode() {
            string xmlPath = "d:\\data\\customer.xml";
            XmlDocument document = new XmlDocument();
            document.Load(xmlPath);

            string deleteCustomerCode = "904063aa-17a9-438a-8f3f-a31641fb230b";
            XmlNode root = document.SelectSingleNode("Customers");
            XmlNodeList nodeList = root.ChildNodes;
            foreach(XmlNode node in nodeList ){
                Console.WriteLine("begin to search..");
                XmlElement childElement = node as XmlElement;
                string customerCode = childElement.GetAttribute("customerCode");
                if(customerCode.Equals(deleteCustomerCode)){
                    Console.WriteLine("searched..Id:"+childElement.GetAttribute("customerId"));
                    root.RemoveChild(childElement);
                    Console.WriteLine("deleteed..");
                }
            }

            document.Save(xmlPath);
        }
    }
}

 

  • 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、付费专栏及课程。

余额充值