M. Fawad Surosh M. Fawad Surosh - 2 months ago 13
C# Question

Inject a node inside XmlNode

I have a quick question:

using C#, how can I inject a node inside XmlNode please. I don't want to involve XmlDocument in my specific method.

Thanks

Answer

If you do not want to pass a XmlDocument instance in your method parameter list, then you can get the XmlDocument from the XmlNode instance by the OwnerDocument property.

Like in this example:

public void MyMethod(XmlNode node)
{
      var newElement = node.OwnerDocument.CreateElement("element");
      node.AppendChild(newElement);
}

Hope this helps.

Comments