C# xml 创建 修改 新增

.NET Framework完全支持XML DOM模式,但它不支持SAX模式。.NET Framework支持两种不同的分析模式:XML DOM分析器(XMLDocument类)和XML阅读器(XMLTextReader类),不支持SAX分析器, 但这并不意味着它没有提供类似SAX分析器的功能。通过XML阅读器可以将SAX的所有的功能很容易的实现及更有效的运用。

但是我们一般运用最多的是DOM。

一、创建xml文件

        using System.Xml;
        using System.IO;//这个一定要写

         XmlDocument xmlDoc = new XmlDocument();
            //加入XML的声明段落,Save方法不再xml上写出独立属性
            xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null));
           //加入根元素
   
            XmlElement root = xmlDoc.CreateElement("ROOT");
            xmlDoc.AppendChild(root);

            XmlElement memberlist = xmlDoc.CreateElement("MEMBERLIST");
          
            XmlElement memberA = xmlDoc.CreateElement("MEMBER");
            memberA.SetAttribute("AGE", ">=29");
            memberA.SetAttribute("SEX", "MALE");
            XmlElement zs = xmlDoc.CreateElement("NAME");
            zs.InnerText = "张三";
            XmlElement ls = xmlDoc.CreateElement("NAME");
            ls.InnerText = "李四";
            memberA.AppendChild(zs);
            memberA.AppendChild(ls);

            XmlElement memberB = xmlDoc.CreateElement("MEMBER");
            memberB.SetAttribute("AGE", ">=35");
            memberB.SetAttribute("SEX", "MALE");
            XmlElement ww = xmlDoc.CreateElement("NAME");
            ww.InnerText = "王五";
            XmlElement zl = xmlDoc.CreateElement("NAME");
            zl.InnerText = "赵六";
            memberB.AppendChild(ww);
            memberB.AppendChild(zl);

            memberlist.AppendChild(memberA);
            memberlist.AppendChild(memberB);
            root.AppendChild(memberlist);
            xmlDoc.Save(xmlpath);            
       

效果如下:

<?xml version="1.0" encoding="UTF-8"?>

<ROOT>
     <MEMBERLIST>
            <MEMBERAGE=">=29"SEX="MALE">
                      <NAME>李四</NAME>
                 *   <NAME/>                  
         </MEMBER>
            <MEMBERAGE=">=35"SEX="MALE">
                        <NAME>王五</NAME>
                        <NAME>赵六</NAME>
           </MEMBER>
    </MEMBERLIST>
</ROOT>
看上面成功倒是成功了,不过由于粗心,在*这行的NAME标签出错了。我想把第一个NAME改为张三,第二个NAME改为李四
二、修改xml文件
  c#代码如下:
 XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlpath);
            XmlNode memberlist = xmlDoc.SelectSingleNode("ROOT/MEMBERLIST");
            XmlNodeList nodelist=memberlist.ChildNodes;
           // XmlNodeList nodelist=xmlDoc.GetElementsByTagName("MEMBER");
            foreach (XmlNode node in nodelist)
            {
                if (node.Attributes["AGE"].InnerText.ToString().Equals(">=29"))
                {
                    node.ChildNodes[0].InnerText = "张三";
                    node.ChildNodes[1].InnerText = "李四";
                    break;
                }
            }
            xmlDoc.Save(xmlpath);
            MessageBox.Show("保存成功");
xml效果如下:
<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
     <MEMBERLIST>
            <MEMBERAGE=">=29"SEX="MALE">
                      <NAME>张三</NAME>
                      <NAME>李四</NAME>                 
          </MEMBER>
            <MEMBERAGE=">=35"SEX="MALE">
                        <NAME>王五</NAME>
                        <NAME>赵六</NAME>
             </MEMBER>
         </MEMBERLIST>
  </ROOT>
这时候,如果增加人员的情况下,我还需要添加节点。
三、xml文件添加节点如下
c#代码如下
 XmlDocument xmlDoc = new XmlDocument();
            if (File.Exists(xmlpath))
            {
                xmlDoc.Load(xmlpath);
                XmlNode memberlist = xmlDoc.SelectSingleNode("ROOT/MEMBERLIST");
                XmlElement member = xmlDoc.CreateElement("MEMBER");
                member.SetAttribute("AGE", "<29");
                member.SetAttribute("SEX", "MAIL");
                XmlElement lq = xmlDoc.CreateElement("NAME");
                lq.InnerText = "六七";
                member.AppendChild(lq);
                memberlist.AppendChild(member);
                xmlDoc.Save(xmlpath);
                MessageBox.Show("添加成功");
            }
            else
            {
                MessageBox.Show("文件不存在");
            }
效果如下:
<<ROOT>
     <MEMBERLIST>
            <MEMBERAGE=">=29"SEX="MALE">
                     <NAME>张三</NAME>
                     <NAME>李四</NAME>                 
          </MEMBER>
            <MEMBERAGE=">=35"SEX="MALE">
                       <NAME>王五</NAME>
                       <NAME>赵六</NAME>
             </MEMBER>
        <MEMBERAGE="<29" SEX="MALE">
                       <NAME>六七</NAME>
             </MEMBER>
         </MEMBERLIST>
< </ROOT>
 有几点要记住了,XmlNode包含XmlElement,即XmlNode可以转化为XmlElement而XmlElement不可以转化为XmlNode.
xmDoc.selectSingleNode("节点名称").这个节点名称是他的路径。若是根节点当时只写名称就可以了,若是子节点,则需要些明从根节点到它的路径。

posted on 2017-03-10 09:31  澜貓  阅读(4231)  评论(0编辑  收藏  举报

导航