How do I import a specific XML node and append it into a different document?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Im trying to insert a copy of an existing node from one document into another but I cant seem to get around this error:
System.ArgumentException was unhandled<br/>
Message=The node to be inserted is from a different document context.<br/>

so heres how I tried to set this up:
// declare "global" values for the class.
private static XmlDocument convert_rules= new XmlDocument();<br/>
private static XmlNode arrow_rules = null;
// in main I get the overall node Im interested in searching by...
XmlTextReader r = new XmlTextReader(convert_rules_path);<br/>
convert_rules.Load(r);<br/>
r.Close();<br/>
arrow_rules = convert_rules.SelectSingleNode("/root/disti[@name=Arrow]");
// determine if this is an established parameter type, then use the established rule<br/>
XmlNode xrule = establishedRule((string)dr.ItemArray[4]);<br/>
if (xrule != null)<br/>
{<br/>
groupNode.AppendChild(XML4Parameter((string)dr.ItemArray[2], xrule)); // this line generates exception <br/>
}
// supporting methods....
private static XmlNode establishedRule(string transim_type)<br/>
{<br/>
XmlNode xmlRule = null;<br/>
XmlNodeList RuleList = arrow_rules.SelectNodes("group/parameters/source/dest[@type=" + transim_type + "]");
if (RuleList.Count == 0) return null
xmlRule = RuleList[0].ParentNode;<br/>
xmlRule = xmlRule.OwnerDocument.ImportNode(RuleList[0].ParentNode, true);<br/>
return xmlRule;<br/>
}
private static XmlNode XML4Parameter(string paramName, XmlNode xrule)<br/>
{<br/>
XmlNode node = xrule;<br/>
node.Attributes["col"].Value = paramName;<br/>
return node;<br/>
}

Ive run out of ideas on what to try to fix this. <hr class="sig Developer Frog Haven Enterprises

View the full article
 
Back
Top