Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net C# Spliting xml file

Tags:

c#

.net

xml

I am using a code from Code Project to split an xml file into multiple files. It is working fine in this below case: "Registrations" is the parent node and when split is among "Registration"

<Registrations>
<Registration xmlns:i="...............">
  <RegistrationID>108260</RegistrationID>
...................
..................
 </Registration>
 <Registration xmlns:i="...............">
  <RegistrationID>108260</RegistrationID>
   ...................
   ..................
 </Registration>
 <Registration xmlns:i="...............">
  <RegistrationID>108260</RegistrationID>
   ...................
   ..................
 </Registration>

But the code is not working when XML file is in this format: "RegistrationOpenData" is the root node, then there is another node "Registrations" and split has to be made among "Registration"

<RegistrationOpenData xmlns:i="............" xmlns="">
<Description>......</Description>
<InformationURL>..........</InformationURL>
<SourceAgency>...............</SourceAgency>
<SourceSystem>...........</SourceSystem>
<StartDate>................</StartDate>
<EndDate i:nil="true" />
<Registrations>
<Registration xmlns:i="...............">
  <RegistrationID>108260</RegistrationID>
...................
..................
 </Registration>
 <Registration xmlns:i="...............">
  <RegistrationID>108260</RegistrationID>
...................
..................
 </Registration>
 <Registration xmlns:i="...............">
  <RegistrationID>108260</RegistrationID>
...................
..................
 </Registration>
</Registrations>
</RegistrationOpenData>

The code that I am using is as below:

private void buttonSPLIT_Click(object sender, EventArgs e)
{
    string sourceFile = @"D:\sample.xml";
    string rootElement = "RegistrationOpenData";
    string descElement = "Registration";
    int take = 1;
    string destFilePrefix = "RegistrationsPart";
    string destPath = @"D:\PART\";

    SplitXmlFile(sourceFile, rootElement, descElement, take,
        destFilePrefix, destPath);

}

private static void SplitXmlFile(string sourceFile
                    , string rootElement
                    , string descendantElement
                    , int takeElements
                    , string destFilePrefix
                    , string destPath)
{          
    XElement xml = XElement.Load(sourceFile);
    // Child elements from source file to split by.
    var childNodes = xml.Descendants(descendantElement);

    // This is the total number of elements to be sliced up into 
    // separate files.
    int cnt = childNodes.Count();

    var skip = 0;
    var take = takeElements;
    var fileno = 0;

    // Split elements into chunks and save to disk.
    while (skip < cnt)
    {
        // Extract portion of the xml elements.
        var c1 = childNodes.Skip(skip)
                        .Take(take);

        // Setup number of elements to skip on next iteration.
        skip += take;
        // File sequence no for split file.
        fileno += 1;
        // Filename for split file.
        var filename = String.Format(destFilePrefix + "_{0}.xml", fileno);
        // Create a partial xml document.
        XElement frag = new XElement(rootElement, c1);
        // Save to disk.
        frag.Save(destPath + filename);
    }
}
like image 872
Karishma Avatar asked Nov 08 '22 13:11

Karishma


1 Answers

I have just tested your code in VS 2015 and it seems to work. It generates 3 XML files with the following content:

<?xml version="1.0" encoding="utf-8"?>
<RegistrationOpenData>
  <Registration>
    <RegistrationID>108260</RegistrationID>
  </Registration>
</RegistrationOpenData>

Is it what you are expecting ? Can you give more details on your issue ?

like image 108
Arnaud Develay Avatar answered Nov 15 '22 07:11

Arnaud Develay