Dear
Friends,
I
have two XML documents as follows:
1. Order.xml
- <order>
-
<purchase>
<item quantity="1">Def Leppard:
Pyromania</item>
<item quantity="1">Ozzy Osbourne:
Goodbye to Romance</item>
</purchase>
-
<shipping>
<to>Shawn Farkas</to>
<street>5 21st Street</street>
<city>Seattle</city>
<state>WA</state>
<zip>98000</zip>
</shipping>
-
<payment>
<card type="visa">0000-0000-0000-0000</card>
<address sameAsShipping="yes"
/>
</payment>
</order>
- Address.xml
- <Address>
<StreetName>Sterling Rd, 1st Cross
Street</StreetName>
<City>Chennai</City>
<Pincode>600034</Pincode>
<State>Tamilnadu</State>
<Country>IN</Country>
</Address>
I
need to insert all the nodes in Address.xml in Order.xml before node
<Shipping>. I tried with the following code but it returns me the
following exception.
“The reference node is not a child of this node.”
Please
help me in solving this problem
Kind
Regards
Arun
Raj.C
The
code is as follows
using System;
using System.Xml;
using System.Xml.XPath;
public class clsConstruct
{
public void ConstructXML()
{
XmlDocument
docLocal = new XmlDocument();
docLocal.Load("order.xml");
XmlDocument
docAddress = new XmlDocument();
docAddress.Load("address.xml");
XmlNode
nodShipping = docLocal.SelectSingleNode("/order/shipping");
docLocal.InsertBefore(docAddress.DocumentElement,
nodShipping);
docLocal.Save("orderaddress.xml");
docLocal
= null;
docAddress
= null;
nodShipping
= null;
}
public static void Main(string[] args)
{
clsConstruct
objClass = new clsConstruct();
objClass.ConstructXML();
objClass
= null;
}
}