日期:2014-05-16 浏览次数:20929 次
<?xml version=“1.0”>
<School>
<Student>
<name>stu1</name>
<id>id1</id>
</Student>
<Professor>
<name>prof1</name>
<id>id2</id>
</Professor>
</School>
<?xml version=“1.0”>
<School>
<Department name="CS">
<Student>
<name>stu1</name>
<id>id1</id>
</Student>
<Professor>
<name>prof1</name>
<id>id2</id>
</Professor>
<Department>
</School>
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"c:\test.xml");
XmlElement newElement = xmlDoc.CreateElement("Department");
newElement.SetAttribute("name", "CS");
for (int i = 0; i < xmlDoc.DocumentElement.ChildNodes.Count; i++)
newElement.AppendChild(xmlDoc.DocumentElement.ChildNodes[i--]);
xmlDoc.DocumentElement.AppendChild(newElement);
xmlDoc.Save(@"c:\result.xml");