在XML父节点的开头添加一个节点
答
据我了解您的问题,您可能正在寻找XmlNode.PrependChild()
方法。
例子:
XmlDocument doc = new XmlDocument();
XmlNode root = doc.DocumentElement;
//Create a new node.
XmlElement node = doc.CreateElement("price");
//Add the node to the document.
root.PrependChild(node);
答
我相信这个问题是问如何将节点添加到XML文件的开头。我以如下方式做到这一点:
// This is the main xml document
XmlDocument document = new XmlDocument();
// This part is creation of RootNode, however you want
XmlNode RootNode = document.CreateElement("Comments");
document.AppendChild(RootNode);
//Adding first child node as usual
XmlNode CommentNode1 = document.CreateElement("UserComment");
RootNode.AppendChild(commentNode1);
//Now create a child node and add it to the beginning of the XML file
XmlNode CommentNode2 = document.CreateElement("UserComment");
RootNode.InsertBefore(commentNode2, RootNode.FirstChild);
这很不清楚你在问什么,或者你尝试过什么。如果你能够提供一个你想要做的事情的小例子,并且显示试图实现它的代码,以及与你想要发生的事情相比发生的事情,那真的会有所帮助。如果可能的话,我还强烈建议使用LINQ to XML,作为比XmlDocument更好的XML API。 –
@TheCoder你解决了你的问题吗? – croxy