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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:20:47+00:00 2026-05-31T19:20:47+00:00

First of all: What I need to achieve is write an Xml document Header,

  • 0

First of all: What I need to achieve is write an Xml document Header, then take some elements out of an existing Xml file and put them into my document Header.

Finally I need to parse some objects into the same file.
The resulting file has to match a specific scheme as it should be imported by a program.

So far I hope it’s understandable.

My problem with that is:

I’ve written my document’s Header and extracted the elements of the second document.
But I can’t manage to write them into my Header file, without getting several Errors when trying to load them again.

Like I stated on top: I need exactly the same tags that I’ve extracted from that second file, without having c# putting in some extras like the declaration tag.


As requested in the “How to Ask” Page I’m going to try to recapitulate all I’ve tried to make this work.

So now for my history of failures 😉
The code below is the one I ended up with after 2 days of trying. I tried some before, but this one is probably the “most” working one:

   XElement rootElement = XElement.Load(@filepath);                

        FileStream fs = new FileStream(@Savepath, FileMode.Append);                              


        IEnumerable<XElement> elements = rootElement.Descendants("Extract1");                    
        foreach (XElement item in elements)
        {
            Console.WriteLine(item);
            item.Save(fs); 
        }                                                                                    


        IEnumerable<XElement> elements2 = rootElement.Descendants("Extract2"); 
        foreach (XElement item in elements2)
        {
            Console.WriteLine(item);
            item.Save(fs);                                                                                       

        }
        fs.Close();

Console Output reads like this:

 <Extract1>
       <childnodes />
 </Extract1>
 <Extract2>
   <childnodes />
 </Extract2>

And that’s exactly what I want in my file, so it’s not like I selected the wrong nodes.
But by saving into the file a magical declaration tag appears, and the Xml looks like the one below.

<?xml version="1.0" encoding="UTF-8"?> 
<Extract1>
    <childnodes />
</Extract1><?xml version="1.0" encoding="UTF-8"?> 
<Extract2>
    <childnodes />
</Extract2>

So I can’t read the files in again.

I tried with

XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;

also. That kills the declaration tag, but I still can’t read the file because now
this section “scores” an error.

<Extract1>
    <childnodes />
</Extract1><Extract2>

The last thing I tried is writing the extracted items in a List listX and serialize them with the code below

XmlSerializer serializer = new XmlSerializer(typeof(List<XElement>));
            TextWriter textWriter = new StreamWriter(@savepath);
            serializer.Serialize(textWriter, listX);
            textWriter.Close();

but now I get these

<ArrayOfXElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<XElement>

To summarize my problem: is there any way to read these Nodes out and “paste” them into a different file, without adding extra tags?

By the way, I thought of keeping the original Xml file and just deleting all the Nodes I don’t want, but first that might take a long time as these are pretty big Xml files, and second, they are partially generated automatically so I don’t have each Name.

  • 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-05-31T19:20:48+00:00Added an answer on May 31, 2026 at 7:20 pm

    When you write (Save()) an XElement, it assumes you want to write a complete XML document so it adds the tag.

    The best solution would probably be to add them to an XDocument and save it all at once when finished.

    But a short solution, minimal change form your current code:

    Console.WriteLine(item);
    //item.Save(fs); 
    fs.WriteLine(item.ToString());
    

    But you will have trouble surrounding this with a group (root) tag.

    An alternative based on your listX:

    var doc  = new XElement("Extracts", listX);
    // add/insert/replace other elements
    doc.Save(filePath);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to implement program by c# application. first of all I need web
First of all, I don't need a textual comparison so Beyond Compare doesn't do
All I need this for is strcpy(). I want to see whether the first
First of all I'm not sure this is even possible, however I need to
i need to redirect all of the stdout of a program except the first
First of all, I have following directory structure: /project_directory/rewriting_experimentation_directory htaccess File realfile.txt What I
$('img, div.name < *') Only returns the first children but i need all of
first of all, I'm not good with jQuery and coding. I'm trying to achieve
First of all, I know how to build a Java application. But I have
First of all, I'm fairly sure snapping to grid is fairly easy, however I've

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.