C# 添加,修改,删除Xml节点

时间:2022-06-29 05:14:38
//添加xml节点
    private void AddXml(string image, string title) 
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(Server.MapPath("../flash/dati.xml"));         XmlNode root = xmlDoc.SelectSingleNode("images");//查找<images>
        XmlElement xe1 = xmlDoc.CreateElement("thumb");//创建一个<thumb>节点
        xe1.SetAttribute("displayNum", "6");//设置该节点displayNum属性
        xe1.SetAttribute("separation", "5");//设置该节点separation属性
        XmlElement xesub1 = xmlDoc.CreateElement("image");
        xesub1.InnerText = image;//设置文本节点
        xe1.AppendChild(xesub1);//添加到thumb节点中
        XmlElement xesub2 = xmlDoc.CreateElement("description");
        xesub2.InnerText = title;
        xe1.AppendChild(xesub2);
        root.AppendChild(xe1);//添加到<images>节点中
        xmlDoc.Save(Server.MapPath("../flash/dati.xml"));
    }
  public void WriteXml(string FileName, string name, string age, string sex)
    {
        //初始化XML文档操作类
        XmlDocument myXml = new XmlDocument();

        //加载指定的XML文件
        myXml.Load(FileName);

        //添加元素-姓名
        XmlElement ele0 = myXml.CreateElement("name");
        XmlText Text0 = myXml.CreateTextNode(name);

        //添加元素-年龄
        XmlElement ele1 = myXml.CreateElement("age");
        XmlText Text1 = myXml.CreateTextNode(age);

        //添加元素-性别
        XmlElement ele2 = myXml.CreateElement("sex");
        XmlText Text2 = myXml.CreateTextNode(sex);

        //添加元素的节点---studentRecord
        XmlNode newElem = myXml.CreateNode("element", "studentRecord", "");

        //在节点中添加元素
        newElem.AppendChild(ele0);
        newElem.LastChild.AppendChild(Text0);
        newElem.AppendChild(ele1);
        newElem.LastChild.AppendChild(Text1);
        newElem.AppendChild(ele2);
        newElem.LastChild.AppendChild(Text2);

        //将节点添加到文档中
        XmlElement root = myXml.DocumentElement;
        root.AppendChild(newElem);

        //保存所有的修改
        myXml.Save(FileName);

    }

           //删除节点内容
    private void DelXml(string image)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(Server.MapPath("../flash/dati.xml"));

        XmlNodeList xnl = xmlDoc.SelectSingleNode("images").ChildNodes;
        
        foreach (XmlNode xn in xnl)
        {
            XmlElement xe = (XmlElement)xn;
            if (xe.InnerText.IndexOf(image,0) >= 0)
            {
                xn.ParentNode.RemoveChild(xn);
               // xn.RemoveAll();
            }
        }
        xmlDoc.Save(Server.MapPath("../flash/dati.xml"));
    }
  //修改 foreach(XmlNode xn in nodeList)//遍历所有子节点
   {
    XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
    if(xe.GetAttribute("genre")=="")// 判读条件
    {
     xe.SetAttribute("genre",newStr);//则修改该属性为newstr 
     XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点
     foreach(XmlNode xn1 in nls)//遍历
     {
      XmlElement xe2=(XmlElement)xn1;//转换类型
      if(xe2.Name=="author")//如果找到 //判读条件
      {
       xe2.InnerText=newText;//则修改 
       break;//找到退出来就可以了
      }
     }
     break;
    }
    //写XML

        private void WriteXML()
        {
            XmlTextWriter xtw = new XmlTextWriter(@"MessageBook\Data\MessageBook1.xml"), null);
            xtw.Formatting = Formatting.Indented;

            xtw.WriteStartDocument();
            //xtw.WriteComment();

            xtw.WriteStartElement("Item");
            xtw.WriteElementString("UserName", "Tom");

            xtw.WriteEndElement();

            xtw.WriteEndDocument();
            xtw.Close();
        }

 

 

//读XML

private void ReadXML()

{

// 创建一个XmlTextReader类的对象并调用Read方法来读取XML文件
            XmlTextReader textReader  = new XmlTextReader("C:\\books.xml");
            textReader.Read();
            
// 节点非空则执行循环体
            while ( textReader.Read() )
            
{
                
// 读取第一个元素
                textReader.MoveToElement();
               Console.WriteLine(
"XmlTextReader Properties Test");
                Console.WriteLine(
"===================");  

                
// 读取该元素的属性并显示在控制台中
                Console.WriteLine("Name:" + textReader.Name);
                Console.WriteLine(
"Base URI:" + textReader.BaseURI);
                Console.WriteLine(
"Local Name:" + textReader.LocalName);
                Console.WriteLine(
"Attribute Count:" + textReader.AttributeCount.ToString());
                Console.WriteLine(
"Depth:" + textReader.Depth.ToString());
                Console.WriteLine(
"Line Number:" + textReader.LineNumber.ToString());
                Console.WriteLine(
"Node Type:" + textReader.NodeType.ToString());
                Console.WriteLine(
"Attribute Count:" + textReader.Value.ToString());
}