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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:08:25+00:00 2026-05-23T05:08:25+00:00

We have a product which uses ‘XmlTextWriter’ in C# – .NET 2.0 to create

  • 0

We have a product which uses ‘XmlTextWriter’ in C# – .NET 2.0 to create a large number of small XML Files. These files are then repeatedly read using ‘XmlTextReader’.

We have found that in very rare cases on a few customer machines the contents of the XML File is replaced by a large number of white spaces. Once this happens the ‘XmlTextReader’ will obviously fail to read the XML File with the error “Root Element is missing”.

Here are the logic details:

  1. When writing a new Xml File – the file is first written to a temporary folder using :

    XmlTextWriter xDoc = new XmlTextWriter(Path, Encoding.UTF8);
    
  2. Once the file is written to the temporary folder – ‘XmlTextReader’ is used to verify the file.

  3. If and only if the file is verified then it is copied to the final location.

  4. Over a few days the file is read multiple times using:

    XmlTextReader xDoc = new XmlTextReader(Path);
    
  5. In some rare cases the reader fails with the error ‘Root Element is Missing’ and we see that the XML File now contains a number of whitespaces and no XML data.

Here are some code extracts:

This code is used for the serialization.
(Keep in mind that the serialization is done to a temporary folder and only copied to the final location once the temporary XmlFile is verified.)

            public void SerializeWithXmlTextWriter(XMLMetaData instance, string Path)
    {
        instance.CommitLists();

        #region XmlTextWriter

        XmlTextWriter xDoc = null;

        try
        {
            xDoc = new XmlTextWriter(Path, Encoding.UTF8);
            xDoc.Formatting = Formatting.Indented;

            xDoc.WriteProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\"");
            xDoc.WriteStartElement("MD");
            xDoc.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
            xDoc.WriteAttributeString("xmlns", "xsd", null, "http://www.w3.org/2001/XMLSchema");


// A number of other elements are written here

            xDoc.WriteEndElement();
        }
        finally
        {
            if (xDoc != null)
            {
                xDoc.Close();
                xDoc = null;
            }
        }

        #endregion
    }

This code is used for the deserialization.
(it is also used to verify the file after serialization.)

            public XMLMetaData DeserializeWithXmlTextReader(string Path)
    {
        XMLMetaData instance = new XMLMetaData();

        #region XmlTextReader

        XmlTextReader xDoc = null;

        try
        {
            xDoc = new XmlTextReader(Path);



            while (xDoc.Read())
            {
                switch (xDoc.Name)
                {
                    //Each element is processed using a case statement
                    //Omitted from this code sample
                }
            }
        }
        finally
        {
            if (xDoc != null)
            {
                xDoc.Close();
                xDoc = null;
            }
        }

        #endregion

        return instance;
    }

We’ve been trying to solve this issue for a number of months and cannot get anywhere since it only happens on a few client machines out of thousands. We have never been able to replicate it on our development and test machines.

We have had reports of the issue vanishing when backup applications are stopped. We also have a customer who only seems to have issues when running Visual Studio.

Also for customers with this problem – it only seems to happen every few weeks!

Thanks in advance for your help 🙂

Simon

  • 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-23T05:08:26+00:00Added an answer on May 23, 2026 at 5:08 am

    You state that the file is verified, then copied, and that the verified file later breaks. I can see at least four possibilities:

    1. Race condition. The problem occurs because you’re reading the file at the same time as copying a new version to replace it.
    2. The copying process is what goes wrong.
    3. The file is successfully copied and later blanked by some other process.
    4. The file is successfully copied and later blanked by a bug in XmlTextReader while you’re reading the config.

    The solution to 1 is to use cross-process synchronisation – e.g. semaphores.

    To investigate 2, you can check the file after copying.

    To be paranoid about 3 and 4 you can ensure that the copy is created read-only, and for 4 you can pass XmlTextReader a FileStream opened read-only rather than a path.

    If none of these help, at least you’ll have ruled out a few possibilities.

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

Sidebar

Related Questions

We have a dot net 2.0 based C# product which uses Mysql as the
I have a product table which simplifies to this: create table product(id int primary
I have an ASP.Net website which uses a MySQL database for the back end.
I have an ASP.NET project that uses XML Serialization for the main operation for
I have an app which uses two sliders to generate a product used elsewhere
I have a desktop product which uses an embedded webserver which will use self-signed
I have written an installer using WIX for a product, which uses: custom extensions
We have a Java product that uses a lot of FOSS/COTS software. A number
I have a product which has been traditionally shipped as an MSI file. It
I have a field 'Description' which can have product descriptions with any unicode characters.

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.