XML files that are serialized using XmlTextWriter and then read using XmlTextReader are sometimes damaged

We have a product that uses "XmlTextWriter" in C # -NET 2.0 to create a large number of small XML files. These files are then re-read using the "XmlTextReader".

We found that in very rare cases on several client machines, the contents of the XML file are replaced by a large number of white spaces. Once this happens, the "XmlTextReader" will obviously not be able to read the XML file with the error "Root Element is missing."

Here are the logical details:

  • When writing a new Xml file, the file is first written to a temporary folder using:

    XmlTextWriter xDoc = new XmlTextWriter(Path, Encoding.UTF8);
    
  • As soon as the file is written to a temporary folder, "XmlTextReader" is used to check the file.

  • If and only if the file is verified, it is copied to the final location.

  • For several days, the file is read several times using:

    XmlTextReader xDoc = new XmlTextReader(Path);
    
  • In some rare cases, the reader fails with the "Root Element is Missing" error, and we see that the XML file now contains several spaces and XML data.

Here are some code snippets:

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

            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 deserialization.
(It is also used to check 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;
    }

, . .

. , Visual Studio.

- , , !

:)

+3
3

, , . :

  • . - , , .
  • - .
  • - .
  • ​​XmlTextReader, .

1 - , .

2, .

3 4, , , 4 XmlTextReader, FileStream , .

, , .

0

, , , .Net 2.0.

temp , "XmlTextWriter" ISO-8859-1, . 0.

Flush , .

, 4000 , . - , , .

, , , .

Formatting.Indented, , .

:

    Public Sub Save(ByVal st As Stream, Optional ByVal AttachComment As Boolean = True)

    Dim xtw As New XmlTextWriter(st, Text.Encoding.GetEncoding("ISO-8859-1"))

    xtw.Formatting = Formatting.Indented

    xtw.WriteStartDocument()

    'Header.
    If AttachComment Then
        xtw.WriteComment(GenerateCreationComment())
    End If

    xtw.WriteStartElement("SektionsdataFile")

    xtw.WriteStartElement("Header")
    WriteStringElement(xtw, "FileType", "Settings")
    WriteStringElement(xtw, "FormatVersion", CurrentFormatVersion)
    xtw.WriteEndElement()

    'Settings.
    xtw.WriteStartElement("Settings")

    SaveSettingsCategory(xtw, Application)
    SaveSettingsCategory(xtw, Behaviour)
    SaveSettingsCategory(xtw, Calculation)
    SaveSettingsCategory(xtw, Forms)
    SaveSettingsCategory(xtw, Hardware)
    SaveSettingsCategory(xtw, Layout)
    SaveSettingsCategory(xtw, License)
    SaveSettingsCategory(xtw, Paths)
    SaveSettingsCategory(xtw, Printing)

    xtw.WriteEndElement()

    xtw.WriteEndElement()

    xtw.WriteEndDocument()

    xtw.Flush()

    xtw.Close()

End Sub
+1

using(XmlWriter xmlw = XmlWriter.Create(String, XmlWriterSettings))
{
  // your code here
}

xml. , , ( ). using XML .

XmlTextReader Microsoft (. ).

0

All Articles