I have loaded XmlDocument into memory and created new XmlElement. Now I am trying to add XmlElement to the path /report/section/hosts but I don't know how. I can add it easily below root node of XML but I cannot figure out how can I navigate deeper level in XML and just append there. In pseudo I am trying to do this:
我已将XmlDocument加载到内存中并创建了新的XmlElement。现在我正在尝试将XmlElement添加到路径/ report / section / hosts中,但我不知道如何。我可以在XML的根节点下轻松添加它,但我无法弄清楚如何在XML中导航更深层次并且只是追加到那里。在伪我试图这样做:
doc.SelectNodes("/report/section/hosts").AppendChild(subRoot);
The code:
XmlDocument doc = new XmlDocument();
doc.Load("c:\\data.xml");
//host
XmlElement subRoot = doc.CreateElement("host");
//Name
XmlElement ElName = doc.CreateElement("name");
XmlText TxtName = doc.CreateTextNode("text text");
ElName.AppendChild(TxtName);
subRoot.AppendChild(ElName);
doc.DocumentElement.AppendChild(subRoot);
doc.Save("c:\\data.xml");
4 个解决方案
#1
Try SelectSingleNode instead of SelectNodes
尝试使用SelectSingleNode而不是SelectNodes
XmlElement parent = (XmlElement)doc.SelectSingleNode("/report/section/hosts")
parent.AppendChild(subRoot);
#2
You are almost there. Try using SelectSingleNode instead:
你快到了。请尝试使用SelectSingleNode:
XmlNode node = doc.SelectSingleNode("/report/section/hosts");
node.AppendChild(subRoot);
#3
The SelectNodes method returns a list of Nodes. You should use SelectSingleNode instead...
SelectNodes方法返回节点列表。你应该使用SelectSingleNode代替......
e.g. (top of my head, did not test in Visual Studio)
例如(我的头脑,没有在Visual Studio中测试)
doc.SelectSingleNode("/report/section/hosts").AppendChild(subRoot);
#4
You need to get a reference to an XmlElement in your doc (other than the root) to append to. There are a number of methods available on XmlDocument such as GetElementById
and SelectSingleNode
which do this for you in different ways, research to taste.
您需要获取对您要附加到的doc(除root之外)的XmlElement的引用。 XmlDocument上有许多可用的方法,例如GetElementById和SelectSingleNode,它们以不同的方式为您完成这项研究。
That said, the whole API in this area is generally regarded as a bit painful, do you have LINQ available?
也就是说,这个领域的整个API通常被认为有点痛苦,你有LINQ可用吗?
#1
Try SelectSingleNode instead of SelectNodes
尝试使用SelectSingleNode而不是SelectNodes
XmlElement parent = (XmlElement)doc.SelectSingleNode("/report/section/hosts")
parent.AppendChild(subRoot);
#2
You are almost there. Try using SelectSingleNode instead:
你快到了。请尝试使用SelectSingleNode:
XmlNode node = doc.SelectSingleNode("/report/section/hosts");
node.AppendChild(subRoot);
#3
The SelectNodes method returns a list of Nodes. You should use SelectSingleNode instead...
SelectNodes方法返回节点列表。你应该使用SelectSingleNode代替......
e.g. (top of my head, did not test in Visual Studio)
例如(我的头脑,没有在Visual Studio中测试)
doc.SelectSingleNode("/report/section/hosts").AppendChild(subRoot);
#4
You need to get a reference to an XmlElement in your doc (other than the root) to append to. There are a number of methods available on XmlDocument such as GetElementById
and SelectSingleNode
which do this for you in different ways, research to taste.
您需要获取对您要附加到的doc(除root之外)的XmlElement的引用。 XmlDocument上有许多可用的方法,例如GetElementById和SelectSingleNode,它们以不同的方式为您完成这项研究。
That said, the whole API in this area is generally regarded as a bit painful, do you have LINQ available?
也就是说,这个领域的整个API通常被认为有点痛苦,你有LINQ可用吗?