C # XML add XML node as a child of another other node
I have an XML document with a structure like this:
<Book>
<Title title="Door Three"/>
<Author name ="Patrick"/>
</Book>
<Book>
<Title title="Light"/>
<Author name ="Roger"/>
</Book>
I want to be able to melodramatically add XML nodes to this XML at a specific location. Let's say I wanted to add a node link as a child to the author of the node where that name is Roger.
I think it's best if the function containing this logic is passed to the parameter for the name to add the XML node, please advise and what code do I need to add the XML nodes at a specific location in the XML?
Now I use the method .AppendChild()
, but it doesn't allow me to specify the parent node to add under ...
a source to share
AppendChild
will add the node passed to the node you are calling it with.
So, if you select a Author
node, you can add a new node to it:
XmlNode author = XmlDocument.SelectSingleNode("/Book/Author[@name='Roger']");
author.AppendChild(otherElementToAppend);
a source to share