Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8380253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:24:34+00:00 2026-06-09T16:24:34+00:00

I need to take an XML file and create multiple output xml files from

  • 0

I need to take an XML file and create multiple output xml files from the repeating 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 needs to split the repeating “Animal” and produce 3 files named: Animal_1001.xml, Animal_1002.xml, and Animal_1003.xml

Each output file should contain just their respective element (which will be the root). The id attribute from AnimalsBatch.xml will supply the sequence number for the Animal_xxxx.xml filenames. The id attribute does not need 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 XmlDocument, since it needs to be able to run 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); // Put the wrapper element into outdoc

            outdoc.ImportNode(n, true);   // place the node n into outdoc
            outdoc.AppendChild(n);        // This statement errors:
            // "The node to be inserted is from a different document context."

            strFileName = "Animal_" + strSeq + ".xml";

            outdoc.Save(Console.Out);
            Console.WriteLine();
        }
        Console.WriteLine("END OF PROGRAM:  Press <ENTER>");
        Console.ReadLine();
    }

I think I have 2 problems.

A) After doing the ImportNode on node n into outdoc, I call outdoc.AppendChild(n) which complains: “The node to be inserted is from a different document context.” I do not know if this is a scope issue referencing node n within the ForEach loop – or if I am somehow not using ImportNode() or AppendChild properly. 2nd argument on ImportNode() is set to true, because I want the child elements of Animal (3 fields arbitrarily named Quantity, Adjective, and Name) to end up in the destination file.

B) Second problem is getting the Animal element into outdoc. I’m getting ” but I need ‘ ‘ so I can place node n inside it. I think my problem is how I am doing: outdoc.AppendChild(rootnode);

To show the xml, I’m doing: outdoc.Save(Console.Out); I do have the code to save() to an output file – which does work, as long as I can get outdoc assembled properly.

There is a similar question at: Split XML in Multiple XML files, but I don’t understand the solution code yet. I think I’m pretty close on this approach, and will appreciate any help you can provide.

I’m going to be doing this same task using XmlReader, since I’m going to need to be able to handle large input files, and I understand that XmlDocument reads the whole thing in and can cause memory issues.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:24:35+00:00Added an answer on June 9, 2026 at 4:24 pm

    That’s a simple method that seems what you are looking for

    public void test_xml_split()
    {
        XmlDocument doc = new XmlDocument();
        doc.Load("C:\\animals.xml");
        XmlDocument newXmlDoc = null;
    
        foreach (XmlNode animalNode in doc.SelectNodes("//Animals/Animal"))
        {
            newXmlDoc = new XmlDocument();
            var targetNode = newXmlDoc.ImportNode(animalNode, true);
            newXmlDoc.AppendChild(targetNode);
            newXmlDoc.Save(Console.Out);
            Console.WriteLine();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a code that take's the image path from an XML file using
I need to parse a xml file using JAVA and have to create a
it might sound stupid, but I need a way to take an XML file
How to make EditText in Android to take only numeric input from Java file
I need to take a large file, with lines such as: member: cn=user0001,ou=people And
i need to take some row. They came from sql TARIH (sql column) is
I need to take rows from two separate tables, and arrange them in descending
I have a sample xml file that looks like this: <Books> <Category Genre=Fiction BookName=book_name
I want to take a series of xml files and pull the xml of
I have a large application spread across multiple Spring bean definition xml files. In

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.