我想将一个 XmlNode
添加到另一个 XmlNode
如果它不包含该节点(比较应该基于节点名称及其内容)
System.Xml.XmlDocument doc;
...
XmlNode newNode = doc.CreateElement(name);
newNode.InnerXml = something
XmlNode parentNode = doc.GetElementsByTagName(parentName);
if (parentNode.???? (newNode))
{
parentNode.AppendChild(newNode);
}
我怎样才能检查这个存在? parentNode.ChildNodes
没有 Contain
方法。
最佳答案
我认为这会成功:
private void doSomething()
{
XmlDocument doc = new XmlDocument();
XmlNode newNode = doc.CreateElement("name");
newNode.InnerXml = "something";
XmlNode parentNode = doc.GetElementsByTagName("parentName")[0];
// I just stuck an index on end of above line...
// Note that GetElementsByTagName returns an XmlNodeList
int huh = 0;
foreach (XmlNode n in parentNode.ChildNodes)
{
// If I understood you correctly, you want these checks?
if (n.InnerXml == newNode.InnerXml && n.Name == newNode.Name) huh++;
}
if (huh == 0) parentNode.AppendChild(newNode);
}
关于c# - 如何检查 XML 节点是否存在于另一个节点的子节点中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31076680/