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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:02:03+00:00 2026-05-20T21:02:03+00:00

I’m using XDocument to load and save a config file. Most of the time

  • 0

I’m using XDocument to load and save a config file. Most of the time it works correctly, but there are cases (seemingly random) where it appends extra information after the last tag. Here’s a gist of what’s happening.

Config file pre-run:

<?xml version="1.0" encoding="us-ascii"?>
<local>
</local>

code:

XDocument config = new XDocument();
config = XDocument.Load(new FileStream(@"c:\foo.xml", FileMode.Open, FileAccess.Read));
XElement fileEle = config.Root.Element("files");
XElement statsEle = new XElement("stats");
statsEle.Add(new XElement("one", "two"));
statsEle.Add(new XElement("three", "four"));
.
.
.
fileEle.Add(statsEle);
config.Save(new FileStream(@"c:\foo.xml", FileMode.Create, FileAccess.Write), SaveOptions.None);

config file post-run:

<?xml version="1.0" encoding="us-ascii"?>
<local>
    <files>
        <one>two</one>
        <three>four</three>
    </files>
</local>s>
</local>

Any suggestions? No idea why the extra characters are being added. Sometimes it’s the extra tag, sometimes it’s different characters and sometimes it works correctly. I’ve tried loading/saving using different methods (XMLReader, etc.), adding XML tags different ways.. after X runs they all produce the same error. Thanks for the help!

  • 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-20T21:02:04+00:00Added an answer on May 20, 2026 at 9:02 pm

    You have two FileStream‘s open for the same file concurrently… one for reading foo.xml, and one for overwriting it. That seems very problematic.

    I’d recommend:

    • Disposing of the read FileStream as soon as your XmlDocument is loaded:
    using (FileStream fs = new FileStream(@"C:\foo.xml", FileMode.Open, FileAccess.Read))
    {
        config.Load(fs);
    }
    
    • Use FileMode.Truncate in your write FileStream so that the file is truncated to 0 bytes before you start writing to it.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.