0

I want to split an XML file on the basis of a child node, and show the split file.

McGarnagle
  • 101,349
  • 31
  • 229
  • 260

1 Answers1

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