I load an XmlDocument and then select some nodes into an XmlNodeList instance. If I edit any of those nodes, the XmlDocument will be modified
XmlDocument xd = loadXml();
XmlNodeList xnl = xd.SelectNodes("/root/nodes");
foreach (XmlNode n in xnl)
{
n.InnerText = "";
}
So I understand that modifying the XmlNodeList - modifies the XmlDocument that the node list was taken from.
Is there some way to create a deep copy (I think that's what I need) of the list of nodes into another XmlElement, so that when I modify these nodes they will be independent of the original location where they were copied from?
There is more than one way to skin an xml cat. This is just one.
var xd = new XmlDocument();
xd.LoadXml("<root><nodes><node>1</node><node>2</node></nodes></root>");
var xnl = xd.SelectSingleNode("/root/nodes").Clone();
foreach (XmlNode n in xnl)
{
n.InnerText = "x";
}
Console.Out.WriteLine(xd.OuterXml);
Console.Out.WriteLine("--------------");
Console.Out.WriteLine(xnl.OuterXml);
You would need to create your own copies using .CloneNode. MSDN has an example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With