I need to take an xml file and create some output xml files from duplicate nodes of the input file. The source file "AnimalBatch.xml" looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<Animals>
<Animal id="1001">
<Quantity>One</Quantity>
<Adjective>Red</Adjective>
<Name>Rooster</Name>
</Animal>
<Animal id="1002">
<Quantity>Two</Quantity>
<Adjective>Stubborn</Adjective>
<Name>Donkeys</Name>
</Animal>
<Animal id="1003">
<Quantity>Three</Quantity>
<Color>Blind</Color>
<Name>Mice</Name>
</Animal>
</Animals>
The program should split the repeating "Animal" and create 3 files with the names: Animal_1001.xml, Animal_1002.xml and Animal_1003.xml
Each output file should contain only the corresponding element (which will be the root). The id attribute from AnimalsBatch.xml will contain the sequence number for the Animal_xxxx.xml file names. The id attribute does not have to be in the output files.
Animal_1001.xml:
<?xml version="1.0" encoding="utf-8"?>
<Animal>
<Quantity>One</Quantity>
<Adjective>Red</Adjective>
<Name>Rooster</Name>
</Animal>
Animal_1002.xml
<?xml version="1.0" encoding="utf-8"?>
<Animal>
<Quantity>Two</Quantity>
<Adjective>Stubborn</Adjective>
<Name>Donkeys</Name>
</Animal>
Animal_1003.xml>
<?xml version="1.0" encoding="utf-8"?>
<Animal>
<Quantity>Three</Quantity>
<Adjective>Blind</Adjective>
<Name>Mice</Name>
</Animal>
I want to do this with an XmlDocument, since it should work on .Net 2.0.
My program looks like this:
static void Main(string[] args) { string strFileName; string strSeq; XmlDocument doc = new XmlDocument(); doc.Load("D:\\Rick\\Computer\\XML\\AnimalBatch.xml"); XmlNodeList nl = doc.DocumentElement.SelectNodes("Animal"); foreach (XmlNode n in nl) { strSeq = n.Attributes["id"].Value; XmlDocument outdoc = new XmlDocument(); XmlNode rootnode = outdoc.CreateNode("element", "Animal", ""); outdoc.AppendChild(rootnode);
I think I have 2 problems.
A) After running ImportNode on node n in outdoc, I call outdoc.AppendChild (n), which complains: "The inserted node is a different document context." I donβt know if this is a problem of the area referencing node n in the ForEach loop, or if I am somehow using ImportNode () or AppendChild incorrectly. The 2nd argument in ImportNode () is set to true because I want the child elements of Animal (3 fields, arbitrarily named "Quantity", "Adjective name" and "Name") to fall into the target file.
B) The second problem is getting the Animal element in outdoc. I get `` but I need '', so I can place node n inside it. I think my problem is how I do it: outdoc.AppendChild (rootnode);
To show xml, I do: outdoc.Save (Console.Out); I have code to save () to an output file that works if I can correctly build outdoc.
A similar question arises when: Split XML into multiple XML files , but I still do not understand the solution code. I think that I am very close to this approach and would appreciate any help you can provide.
I am going to accomplish the same task with XmlReader, since I will need to process large input files, and I understand that XmlDocument reads all this and may cause memory problems.