Need help in adding a new XML node

DjMorley

New Member
\[code\]<?xml version="1.0" encoding="utf-8"?><mappings> <mapping> <boothID iskey="true">BMZ</boothID> <clientID iskey="true">FPP</clientID> <SubAccount>PS</SubAccount> <Account>PS</Account> </mapping> <mapping> <boothID isKey="true">CAB</boothID> <clientID isKey="true">PFP</clientID> <SubAccount>MS</SubAccount> <!-- I want to add a new node here, how can I do this --> </mapping> <mapping> <boothID isKey="true">JAX</boothID> <clientID isKey="true">BBG</clientID> <SubAccount>MAX</SubAccount> </mapping></mappings>\[/code\]I want to add a new node as mentioned in the above XML. I tried a lot but I could not succeed.\[code\]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); \[/code\]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.THanks all, XmlDocument xDoc = new XmlDocument(); XmlDocument xmlDoc = new XmlDocument();ERROR: The node to be inserted is from a different document context
 
Back
Top