8

I am trying to split a XML file to multiple small xml files in C#.net and am trying to get the best possible approach to this. Any help on this will be great... Sample example on what I am trying to do...

Source XML document

<?xml version="1.0" standalone="yes"?>
<DATABASE>
  <DOC>
    <DOCID>8510188</DOCID>
    <ISSUE>2010</ISSUE>
    <CAT>Literature and Art</CAT>
    <TITLE>Test</TITLE>
    <TEXT>Test</TEXT>
  </DOC>
  <DOC>
    <DOCID>1510179</DOCID>
    <ISSUE>2012</ISSUE>
    <CAT>Miscellaneous</CAT>
    <TITLE>Test</TITLE>
    <TEXT>Test</TEXT>
  </DOC>
</DATABASE>

Should split to two xml documents as below

1)

<?xml version="1.0" standalone="yes"?>
<DATABASE>
  <DOC>
   <DOCID>8510188</DOCID>
   <ISSUE>2010</ISSUE>
   <CAT>Literature and Art</CAT>
   <TITLE>Test</TITLE>
   <TEXT>Test</TEXT>
  </DOC>
</DATABASE>

2)

<?xml version="1.0" standalone="yes"?>
<DATABASE>
  <DOC>
    <DOCID>1510179</DOCID>
    <ISSUE>2012</ISSUE>
    <CAT>Miscellaneous</CAT>
    <TITLE>Test</TITLE>
    <TEXT>Test</TEXT>
  </DOC>
</DATABASE>
jle
  • 9,316
  • 5
  • 48
  • 67
AML
  • 325
  • 1
  • 8
  • 16

1 Answers1

8

Well, I'd use LINQ to XML:

XDocument doc = XDocument.Load("test.xml");
var newDocs = doc.Descendants("DOC")
                 .Select(d => new XDocument(new XElement("DATABASE", d)));
foreach (var newDoc in newDocs)
{
    newDoc.Save(/* work out filename here */);
}

(I'm assuming you want to save them. Maybe you don't need to. I've tested this just by printing them out to the console instead.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thank you But it gives this error 'System.Collections.Generic.IEnumerable' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?) – AML Aug 18 '12 at 08:14
  • 1
    Have you added System.Linq to your usings-list at the top of your file? – Karl-Johan Sjögren Aug 18 '12 at 08:18
  • 2
    @AML: Include namespace `System.Linq` and `System.XML.Linq` in usings section. – Nikhil Agrawal Aug 18 '12 at 08:19
  • Error again 'System.Collections.Generic.IEnumerable' does not contain a definition for 'Save' and no extension method 'Save' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?) – AML Aug 18 '12 at 08:22
  • 1
    @AML: Sorry, that should be `newDoc.Save`. – Jon Skeet Aug 18 '12 at 08:31