gpt4 book ai didi

c# - 如何检查 XML 节点是否存在于另一个节点的子节点中?

转载 作者:太空宇宙 更新时间:2023-11-03 23:34:20 25 4
gpt4 key购买 nike

我想将一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com