I am using code from Code Project to split an xml file into multiple files. It works fine in this case below: "Registration" is the parent node, and when split is among the "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 does not work when the XML file is in this format: "RegistrationOpenData" is the root node, then there is another node "Registration", and the separation must be performed among the "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 I'm using is 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);
source share