試試這個
// Obtain a document; this method is implemented in
// The Quintessential Program to Create a DOM Document from an XML File
Document doc = parseXmlFile("infilename.xml", false);
// Obtain the root element
Element element = doc.getDocumentElement();
// Create an element with the new name
Element element2 = doc.createElement("newname");
// Copy the attributes to the new element
NamedNodeMap attrs = element.getAttributes();
for (int i=0; i<attrs.getLength(); i++) {
Attr attr2 = (Attr)doc.importNode(attrs.item(i), true);
element2.getAttributes().setNamedItem(attr2);
}
// Move all the children
while (element.hasChildNodes()) {
element2.appendChild(element.getFirstChild());
}
// Replace the old node with the new node
element.getParentNode().replaceChild(element2, element);
或者試試這個
XDocument doc = XDocument.Load("input.xml");
doc.Root.Name = "program";
doc.Save("output.xml");
我使用的是僅限xmldoumnet – user2176150 2013-04-06 06:19:58
不是XML文件。我修改後使用xmldocument – user2176150 2013-04-06 06:20:39
需要保存該文檔。 save()是根據您的要求選擇的重載方法。 – Gajendra 2013-04-06 07:09:18