码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-07-15 12:03:22      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:color   之间   字节   encoding   title   attribute   文本   beginning   str   

 

1.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即可

技术图片
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

 

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

标签:color   之间   字节   encoding   title   attribute   文本   beginning   str   

原文地址:https://www.cnblogs.com/lidaying5/p/11187968.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!