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 8649067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:28:04+00:00 2026-06-12T13:28:04+00:00

I’d like to merge a few xml files. The destination xml is slightly different

  • 0

I’d like to merge a few xml files.
The destination xml is slightly different then the source files. The destination file contains an aditional root element.

For example.

The destination xml:

<?xml version="1.0" encoding="utf-8"?>
<customer ID="A0001" name="customername">
.....
.....
</customer>

Source xml:

<?xml version="1.0" encoding="utf-8"?>
<order number="00001">
    <.....>
    <.....>
    <.....>
</order>

Every source xml file needs to be inserted between <customer ...> and </customer>

The source files can be very large (e.g. 2 Gb).

I can write the destination xml file with the root element and read the source files using XmlTextReader and

string myOrder = textReader.ReadOuterXml();
                        writer.WriteRaw(myOrder );

Result (where every order is a different xml file)

<?xml version="1.0" encoding="utf-8"?>
<customer ID="A0001" name="customername">
    <order number="00001">
        <.....>
        <.....>
        <.....>
    </order>
    <order number="00002">
        <.....>
        <.....>
        <.....>
    </order>
    <order number="00003">
        <.....>
        <.....>
        <.....>
    </order>
</customer>

But i’m afraid of out of memory exeptions for the large files using ReadOuterXml().

Any suggestion ?

  • 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-12T13:28:05+00:00Added an answer on June 12, 2026 at 1:28 pm

    It sounds like in this particular case, assuming all the files are really using UTF-8, you can basically cheat. .NET 4 makes this particularly easy:

    public void MergeFiles(string outputPath, string prefix, string suffix,
                           IEnumerable<string> files)
    {
        File.WriteAllText(outputPath, prefix);
        var lines = files.SelectMany(file => File.ReadLines(file).Skip(1));
        File.AppendAllLines(outputPath, lines);
        File.AppendAllText(outputPath, suffix);
    }
    

    This isn’t quite as efficient as it might be, as it’ll open the output file three times – but it’s written about as simply as I could make it. Note that lines here is lazy – this won’t read the source files completely into memory; it’ll read a line at a time.

    It does rely on each file starting with the XML declaration and being in UTF-8 though. There are far more robust streaming approaches you could use, but if you’re confident of your source format, this is very simple…

    EDIT: Sample usage:

    string prefix = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n"
                  + "<customer ID=\"A0001\" name=\"customername\">";
    MergeFiles("output.xml", prefix, "</customer>", sourceFiles);
    

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an XML file, the creators of it stuck in a bunch social
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace

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.