<?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 文档,我正在创建其他 xml 文档的新节点,因此出现错误。谢谢大家, XmlDocument xDoc = new XmlDocument(); XmlDocument xmlDoc = new XmlDocument();
错误:要插入的节点来自不同的文档上下文