zoukankan      html  css  js  c++  java
  • 2.遍历XML即添加修改节点

    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项目XMLTestXMLTestXMLFile1.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 += "
    " + 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 +="
    "+"</"+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
  • 相关阅读:
    Linux操纵系统下的Oracle数据库编程详解
    Apache Tomcat负载平衡设置要领具体解析
    如何设定实施Java法式圭臬标准的Linux沉静状况1
    如何设定实施Java程序的Linux平安情况2
    Linux下Web效力器架设攻略1
    Linux下jsp情况:apache,tomcat设置装备安排1
    处置Linux下Oracle Tomcat 8080端口辩说
    一步一步跟我学Linux平台下搭建Jsp景象2
    若何设定实行Java步调的Linux安好环境3
    Linux下Apache与Tomcat整合的年夜抵要领
  • 原文地址:https://www.cnblogs.com/lidaying5/p/11187968.html
Copyright © 2011-2022 走看看