I have an xml file that looks like the following and I want to add a parent node after the Insurer section and before the Broker - notice that in the Broker section, there will be multiple elements groupings.. The node in bold is what I want to add - no
elements needed just the two tags.
Thank you very much! Just what I needed. Also, remember to add objDOM.save to save after this update. I wanted to add this in case someone else needs a solution like this.
bharris17
Member
1 Points
5 Posts
add parent node to existing xml - linq
May 31, 2012 07:05 PM|LINK
I have an xml file that looks like the following and I want to add a parent node after the Insurer section and before the Broker - notice that in the Broker section, there will be multiple elements groupings.. The node in bold is what I want to add - no elements needed just the two tags.
<BatchDataSet>
<Insurer>
elements...
</Insurer>
<Brokers>
<Broker>
<broker elements...>
</Broker>
<Broker>
<broker elements...>
</Broker>
</Brokers>
</BatchDataSet>
FarhanK
Contributor
2603 Points
350 Posts
Re: add parent node to existing xml - linq
Jun 01, 2012 07:14 AM|LINK
XmlDocument objDOM = new XmlDocument(); objDOM.Load(@"c:\temp\data.xml"); XmlNodeList nodeList = objDOM.SelectNodes("BatchDataSet/Broker"); objDOM.SelectSingleNode("BatchDataSet").InsertAfter(objDOM.CreateNode(XmlNodeType.Element, null, "Brokers", string.Empty), objDOM.SelectSingleNode("BatchDataSet/Insurer")); foreach (XmlNode n in nodeList) { objDOM.SelectSingleNode("BatchDataSet").RemoveChild(n); objDOM.SelectSingleNode("BatchDataSet/Brokers").InsertAfter(n, null); }Regards,
Farhan Uddin Khan
Breeze Technologies
bharris17
Member
1 Points
5 Posts
Re: add parent node to existing xml - linq
Jun 01, 2012 12:18 PM|LINK
Thank you very much! Just what I needed. Also, remember to add objDOM.save to save after this update. I wanted to add this in case someone else needs a solution like this.
Thanks again!!