Parent XML
<order>
<class/>
<account>
<saving/>
</account>
</order>
I want to import node to parent xml
Node:
<data>
<address/>
</data>
After importing, final xml as
<order>
<class/>
<account>
<saving/>
</account>
<data>
<address/>
</data>
</order>
Please help me here
I tried as below:
XmlDocument doc = new XmlDocument(); doc.LoadXml(childXML.InnerXml); mlNode newNodeDataset = doc.DocumentElement; XmlDocument xdoc = new XmlDocument(); xdoc.LoadXml(parentXML); XmlNode root = xdoc.DocumentElement; xdoc.ImportNode(newNodeDataset, true);
It donesn't throwing any error but it is not importing the node. where i am doing wrong here?
All you need to do is add the element to the existing document root using the Add
method, it seems:
var doc = new XDocument(
new XElement("order",
new XElement("class"),
new XElement("account",
new XElement("saving")
)
)
);
var element = new XElement("data", new XElement("address"));
doc.Root.Add(element);
Result (in doc
):
<order>
<class />
<account>
<saving />
</account>
<data>
<address />
</data>
</order>
See more on this question at Stackoverflow