[英]Need help in adding a new XML node
<?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>
I want to add a new node as mentioned in the above XML. 我想添加一个以上XML中提到的新节点。 I tried a lot but I could not succeed.
我尝试了很多,但未能成功。
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);
The question is solved. 问题解决了。 The problem occured due to my silly mistake.
由于我的愚蠢错误,出现了问题。 Basically there are two xml documnets and I'm creating a new node of other xml documnet due to which the error cames.
基本上有两个xml documnets,由于这个错误,我正在创建其他xml documnet的新节点。 THanks all, XmlDocument xDoc = new XmlDocument();
谢谢,XmlDocument xDoc = new XmlDocument(); XmlDocument xmlDoc = new XmlDocument();
XmlDocument xmlDoc =新的XmlDocument();
ERROR: The node to be inserted is from a different document context 错误:要插入的节点来自其他文档上下文
Use something like this, not ac# person but this should help. 使用这样的东西,而不是ac#人,但这应该有所帮助。 I think insertafter is what you need:
我认为insertafter是您需要的:
XmlNode currNode = xDoc.SelectNodes("/mappings/mapping");
XmlNode elm = xDoc.CreateNode(XmlNodeType.Element,"Account",null);
currNode.InsertAfter(elm, currNode.LastChild);
In order to add a node, consider this example: 为了添加节点,请考虑此示例:
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)));
---> ->
<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>
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 ".
You can write longer .Element ForEx: 您可以编写更长的.Element ForEx:
doc.Element(firstnode).Element(secondnode).Element(thirdnode).SetElementValue(fourthnode,valueoffourth);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.