本文介绍了如何将子节点添加到现有XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML文件如下:

I have one XML file as follow:

<Employees>
 <Employee>
   <EmpId>10</EmpId>
 </Employee>
 <Employee>
  <EmpId>11</EmpId>
 </Employee>
</Employees>





使用C#我只想制作如下文件:





Using C# I just want to make this file as follow:

<Employees>
 <Employee>
   <EmpId>10</EmpId>
    <EName>AA</EName>
 </Employee>
 <Employee>
  <EmpId>11</EmpId>
   <EName>BB</EName>
 </Employee>
</Employees>





感谢你。



Thank U.

推荐答案


XmlDocument doc = new XmlDocument();
           doc.Load("d:\\test\\book.xml");

           XmlElement elmRoot = doc.DocumentElement;

           foreach (XmlNode n in doc.DocumentElement)
           {
               XmlElement newtitle = doc.CreateElement("EName");
               newtitle.InnerText = "SomeText";
              n.AppendChild(newtitle);
           }

            doc.Save("d:\\book.xml");


这篇关于如何将子节点添加到现有XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 12:10
查看更多