I want to split an XML file on the basis of a child node, and show the split file.
Asked
Active
Viewed 608 times
0
-
can you show what you have tried? sample xml? – Shoaib Shaikh May 02 '12 at 07:34
-
what do you mean by Show the split file? – Imran Rizvi May 02 '12 at 07:43
1 Answers
0
Although you have not provided full information , following is the sample code near to your requirement.
It reads main.xml file which contains many child nodes named ChildNode and write seperate file for each child node.
XmlDocument xml= new XmlDocument();
xml.Load(@"c:\main.xml");
XmlNodeList xnRep = xml.DocumentElement.GetElementsByTagName("ChildNode");
string strFileName = "ChildNode";
int intFileCount;
for(int i =0; i <xnRep.Count;i++)
{
//Stores the ChildNode Elements in the Variable
strDest = xnRep[i].InnerXml;
//Create the New File with the name "ChildNode_1.xml" and "ChildNode_3.xml"
XmlWriter xw = XmlWriter.Create(strFileName + "_" + intFileCount + ".xml");
//Write the XML
xw.WriteRaw(strDest.ToString());
xw.Close();
intFileCount++;
}
You can also check similar answer at split xml document into chunks

Community
- 1
- 1

Imran Rizvi
- 7,331
- 11
- 57
- 101