需要帮助添加新的XML节点

时间:2012-05-01 13:09:56

标签: c# .net xml

<?xml version="1.0" encoding="utf-8"?>
<mappings>
  <mapping>
    <aID iskey="true">ABC</aID>
    <bID iskey="true">DEF</bID>
    <SubAccount>PS</SubAccount>
    <Account>PS</Account>
  </mapping>
  <mapping>
    <aID isKey="true">GHI</aID>
    <bID isKey="true">PFP</bID>
    <SubAccount>MS</SubAccount>
    <!-- I want to add a new node here, how can I do this  -->
  </mapping>
  <mapping>
    <aID isKey="true">MNO</aID>
    <bID isKey="true">BBG</bID>
    <SubAccount>MAX</SubAccount>
  </mapping>
</mappings>

我想添加上面XML中提到的新节点。我尝试了很多但是我没能成功。

XmlDocument xDoc = new XmlDocument();
xDoc.Load(filename);

foreach (XmlNode node in xmlDoc.SelectNodes("/mappings/mapping"))
{
    if (boothIDNode.InnerXml == BoothID)
    {
        chkBoothIDExists = true;
        if (clientIDNode.InnerText == ClientID)
        {
            chkClientIDExists = true;
            for (int j = 2; j < nodelist.Count; j++)
            {
                columnNode = nodelist[j];
                if (columnNode.Name == column.ToString())
                {
                    XmlNode elm = xDoc.CreateNode(XmlNodeType.Element,"Account",null);
                    elm.InnerText = value.ToString();                                                                                 
                    node.AppendChild(elm);  //  Error comes here 
                }
            }
        }
    }   
}

xmlDoc.Save(filename);  

问题解决了。由于我的愚蠢错误,问题出现了。基本上有两个xml documnet,我正在创建一个其他xml documnet的新节点,因为它出现了错误。 谢谢所有,  XmlDocument xDoc = new XmlDocument();   XmlDocument xmlDoc = new XmlDocument();

错误:要插入的节点来自不同的文档上下文

3 个答案:

答案 0 :(得分:2)

使用这样的东西,而不是c#人,但这应该有所帮助。我认为insertafter是你需要的:

  XmlNode currNode = xDoc.SelectNodes("/mappings/mapping");  
  XmlNode elm = xDoc.CreateNode(XmlNodeType.Element,"Account",null);
  currNode.InsertAfter(elm, currNode.LastChild);

答案 1 :(得分:1)

要添加节点,请考虑以下示例:

              XDocument a = XDocument.Parse(@"<?xml version=""1.0"" encoding=""utf-8""?>
<mappings>
  <mapping>
    <aID iskey="true">ABC</aID>
    <bID iskey="true">FPP</bID>
    <SubAccount>PS</SubAccount>
    <Account>PS</Account>
  </mapping>
  <mapping>
    <aID isKey="true">GHI</aID>
    <bID isKey="true">PFP</bID>
    <SubAccount>MS</SubAccount>
    <!-- I want to add a new node here, how can I do this  -->
  </mapping>
  <mapping>
    <aID isKey="true">MNO</aID>
    <bID isKey="true">BBG</bID>
    <SubAccount>MAX</SubAccount>
  </mapping>
</mappings>");
a.Descendants("mapping").Skip(1).First().Add(new XElement("aaa", new XAttribute("id", 1)));

---&GT;

      <mappings>
  <mapping>
    <aID iskey="true">ABC</aID>
    <bID iskey="true">FPP</bID>
    <SubAccount>PS</SubAccount>
    <Account>PS</Account>
  </mapping>
  <mapping>
    <aID isKey="true">GHI</aID>
    <bID isKey="true">PFP</bID>
    <SubAccount>MS</SubAccount>
    <!-- I want to add a new node here, how can I do this  -->
    <aaa id="1" />
  </mapping>
  <mapping>
    <aID isKey="true">MNO</aID>
    <bID isKey="true">BBG</bID>
    <SubAccount>MAX</SubAccount>
  </mapping>
</mappings>

答案 2 :(得分:0)

XDocument doc = XDocument.Load(filepath); // filepath is string
doc.Element(firstnodename).SetElementValue(descendantname,newvalue); //names and value are string
doc.Save(filepath); // This line optional. And filepath like : @"C:\Users\user\desktop\a.xml" or "C:\\Users\\user\\desktop\\a.xml". You should write with ".

你可以写更长的.Element 外汇:

doc.Element(firstnode).Element(secondnode).Element(thirdnode).SetElementValue(fourthnode,valueoffourth);