将克隆节点追加到相同的XML文档

本文关键字:XML 文档 节点 追加 | 更新日期: 2023-09-27 18:17:29

我试图添加一个克隆节点,让我们说

<Property Id="3" Name="Deadline"></Property>

插入到类名为"AlphaCertificationsIndividual"的同一文档中,但编译器给了我这个错误:要插入的节点来自不同的文档上下文

<Root>
  <Class Name="ECMInstruction" Style="Top">
    <Entity Id="1" Name="DocumentInformation" />
    <Entity Id="2" Name="CustomerInformation" />
    <Property Id="1" Name="DocumentTitle">
    </Property>
    <Property Id="2" Name="DateCreated">
      <Lists>
        <ListName>ws_Users</ListName>
        <ListName>dfdfdfd</ListName>
      </Lists>
    </Property>
    <Property Id="3" Name="Deadline">
    </Property>
  </Class>
  <Class Name="AlphaCertificationsIndividual" Style="Top">
    <Entity Id="1" Name="DocumentInformation" />
    <Property Id="1" Name="DocumentTitle">
    </Property>
    <Property Id="2" Name="DateCreated">
      <Lists>
        <ListName>ws_Users</ListName>
        <ListName>dfdfdfd</ListName>
      </Lists>
    </Property>
    <Property Id="3" Name="Deadline">
    </Property>
  </Class>
</Root>

使用的代码:

    XmlDocument xmldoc = new XmlDocument();
    xmldoc.Load("sample.xml");
    foreach (string id in properties)
    {
        XmlNode props = xmldoc.DocumentElement.SelectSingleNode("//Class[@Name='" + curClass + "']/Property[@Id='" + id + "']");
        XmlNode cloneNode = props.CloneNode(true);
        foreach (var item in dcList.SelectedItems)
        {
            XmlNodeList classes = commonMethods.LoadDocument(xml).DocumentElement.SelectNodes("//Class[@Name='" + item + "']/Property[last()]");
            foreach (XmlNode c in classes)
            {
                String propertyid = c.Attributes["Id"].Value.ToString();
                int.TryParse(propertyid, out value);
                value = value + 1;
                cloneNode.Attributes["Id"].Value = value.ToString();
                c.ParentNode.AppendChild(xmldoc.ImportNode(cloneNode,true));
                xmldoc.Save("sample.xml");
            }
        }
    }

将克隆节点追加到相同的XML文档

我不确定这是否是一个错别字,但似乎你正在调用commonMethods。LoadDocument方法对一个名为xml的变量和get类变量。然后,在附加之前在xmlDoc上调用ImportNode。节点需要导入到将追加子节点的文档对象中。所以,如果你想在xmlDoc中添加,你应该导入到xmlDoc中。

我擅自将代码重写为一个扩展方法,该方法允许您放入fileName, originalClassName, newClassName,要复制的节点的名称及其ID-attributevalue。

public static bool CopyNode(string fileName, string originalClassName, string newClassName, string nodeName, string ID)
{
    XDocument doc = XDocument.Load(fileName);
    if(doc == null)
        throw new ArgumentNullException("doc");
    XElement originalClassElement = doc.Root.Descendants().FirstOrDefault(e => e.Name == "Class" && e.Attribute("Name").Value == originalClassName);
    if (originalClassElement == null)
        return false;
    XElement elementToCopy = originalClassElement.Elements().FirstOrDefault(e => e.Name == nodeName && e.Attribute("Id").Value == ID);
    if (elementToCopy == null)
        return false;
    XElement newClassElement = doc.Root.Descendants().FirstOrDefault(e => e.Name == "Class" && e.Attribute("Name").Value == newClassName);
    if (newClassElement == null)
        return false;
    newClassElement.Add(elementToCopy);
    doc.Save(fileName);
    return true;
}

如果节点已被正确复制,该方法返回true。您已经获得了一些可扩展性,并且可以从具有任何名称的类中复制节点;以及任何你想要复制的节点(注意它们必须有一个ID)。