2.遍历XML即添加修改节点

时间:2022-06-29 05:15:14

 

1.xml

2.遍历XML即添加修改节点2.遍历XML即添加修改节点
<?xml version="1.0" encoding="utf-8" ?>
<stories>
  <story ac="98">
    <title>A House in Aungier Street</title>
    <author>
      <name>Sheridan le Fanu</name>
      <nationality>Irish</nationality>
    </author>
    <rating>eerie</rating>
  </story>
  <story ac ="97">
    <title>The Signalman</title>
    <author>
      <name>Charles Dickens</name>
      <nationality>English</nationality>
    </author>
    <rating>atmospheric</rating>
  </story>
  
</stories>
View Code

 

代码,添加一个按钮,并绑定到btnLoop_Click方法,添加lable txtResult即可

2.遍历XML即添加修改节点2.遍历XML即添加修改节点
private void btnLoop_Click(object sender, EventArgs e)
        {

            XmlDocument doc = new XmlDocument();
            string filePath =@"D:\文档\VS2010项目\XMLTest\XMLTest\XMLFile1.xml";
            doc.Load(filePath);
            
            //添加节点
            XmlElement newBook = doc.CreateElement("story");    
            XmlElement newTitle = doc.CreateElement("Title");
            XmlElement newAuthor = doc.CreateElement("author");
            XmlElement newName = doc.CreateElement("name");
            XmlElement newNationality = doc.CreateElement("nationality");
            XmlElement newRating =doc.CreateElement("rating");

            XmlText title = doc.CreateTextNode("Beginning VS2010");
            XmlText name = doc.CreateTextNode("Karing");
            XmlText nationality = doc.CreateTextNode("UK");
            XmlText rating = doc.CreateTextNode("4 star");
            XmlComment comment = doc.CreateComment("The Previous Version");

            newBook.AppendChild(comment);
            newBook.AppendChild(newTitle);
            newBook.AppendChild(newAuthor);
            newAuthor.AppendChild(newName);
            newAuthor.AppendChild(newNationality);
            newBook.AppendChild(newRating);

            newTitle.AppendChild(title);
            newName.AppendChild(name);
            newNationality.AppendChild(nationality);
            newRating.AppendChild(rating);

            XmlNode root = doc.DocumentElement;
            root.InsertAfter(newBook, root.FirstChild);//放到第一个子节点之后

            doc.Save("xmlFileAddNode.xml");

            //删除节点
            root.RemoveChild(root.FirstChild);//删除第一本书
            doc.Save("deleteNode.xml");


            //在不遍历xml文档的情况下选择节点
            txtResult.Text = FormatText(root, "", "");//遍历xml
        }

        //通过循环xml节点把xml写入字符串
        private string FormatText(XmlNode node, string text, string indent)
        {
            if (node is XmlText)
            {
                text += node.Value;
                return text;
            }

            if (string.IsNullOrEmpty(indent))
                indent = "";
            else 
                text += "\r\n" + indent;


            if (node is XmlComment)//注释节点
            {
                text += node.OuterXml;//OuterXml:当前节点的XML,InnerXml:当前节点开始标签和结束标签之间的XML,Value 只有XmlText,XmlAttribute,XmlComment有值,文本节点的文本值或者属性值
                return text;//注InnerText返回当前节点所有字节的文本,如<book><title>111</title><author>222</author></book>,book节点的InnerText是111222
            }

            text += "<" + node.Name;
            if (node.Attributes.Count > 0)
            { 
                AddAttributes(node,ref text);//属性
            }

            if (node.HasChildNodes)
            {
                text += ">";
                foreach (XmlNode child in node.ChildNodes)
                {
                   text = FormatText(child, text, indent + " ");
                }

                if(node.ChildNodes.Count ==1&&(node.FirstChild is XmlText || node.FirstChild is XmlComment))
                    text +="</"+node.Name +">";
                else 
                    text +="\r\n"+"</"+node.Name +">";
            }else
                text +=" />";
            
            return text;
        }

        private void AddAttributes(XmlNode node ,ref string text)
        {
            foreach (XmlAttribute xa in node.Attributes)
            {
                text += " " + xa.Name + "='" + xa.Value + "'";
            }
        }
View Code