none
XmlDocument.Load (string) to XmlDocument.Load (XmlReader) regression ? RRS feed

  • Question

  • Hello,

    I'm updating old code loading a simple XML file :

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <root>
      <Percents>0.006 0.3893 </Percents>
      <Interval>.083333</Interval>
    </root>

    This code is working :

    XmlDocument docXML2 = new XmlDocument() { XmlResolver = null };
    docXML2.Load(StoragePath);

    Where StoragePath is the full path of file. It's a string.

    Code analysis reports CA3075 unsafe override of Load() method.

    I've updated the code to this :

    XmlDocument docXML = new XmlDocument() { XmlResolver = null };
    System.IO.StringReader sreader = new System.IO.StringReader(StoragePath);
    using (XmlReader reader = XmlReader.Create(sreader, new XmlReaderSettings() { XmlResolver = null }))
      {
        docXML.Load(reader); 
    
        // other things
      }

    But now, the docXML.Load (reader) throw an exception :

    System.Xml.XmlException : 'Données non valides au niveau racine. Ligne 1, position 1.'

    I don't understand why ? :/

    Can someone explain me ?

    Thanks,

    Vincent

    Saturday, March 28, 2020 2:59 PM

Answers

  • Hi Duvernet,

    I researched the issue and most of it goes back to the (Byte Order Marker) or (BOM) 

    The Byte Order Marker (BOM) is a series of byte values placed on the beginning of an encoded text stream (or file). This data allows the reader to correctly decide which character encoding to use when decoding the stream back into a sequence of characters. The use of byte order markers within files and streams is not specific to XML, but its typical to see them in use when XML data is streamed or stored in files.
    https://www.liquid-technologies.com/XML/ByteOrderMarker.aspx

    The Byte Order Marker (BOM) UTF-8 BOM is a sequence of Bytes at the start of a text-stream (0xEF,0xBB,0xBF) that allows the reader to more reliably guess a file as being encoded in UTF-8.
    https://en.wikipedia.org/wiki/Byte_order_mark

    https://neodynamic.wordpress.com/2012/10/11/xmlreader-exception-on-utf-8-xml-strings-with-byte-order-mark-bom/

    if you remove this line

    System.IO.StringReader sreader = new System.IO.StringReader("simple.xml");


    to

     System.IO.TextReader sreader = new StreamReader("simple.xml");

    every things will be fine

     XmlDocument docXML = new XmlDocument() { XmlResolver = null };
              
                System.IO.TextReader sreader = new StreamReader("simple.xml");
    
              
                using (XmlReader reader = XmlReader.Create(sreader, new XmlReaderSettings() { XmlResolver = null }))
                {
                     
                    docXML.Load(reader);
    
                    Console.WriteLine(docXML.InnerXml);
    
                }

    Result


    Please remember to mark the replies as answers if they helped you :) ~


    • Edited by Rebin Qadir Saturday, March 28, 2020 6:54 PM correction text
    • Marked as answer by Duvernet Vincent Saturday, March 28, 2020 8:36 PM
    Saturday, March 28, 2020 6:52 PM

All replies

  • Hi Duvernet,

    I researched the issue and most of it goes back to the (Byte Order Marker) or (BOM) 

    The Byte Order Marker (BOM) is a series of byte values placed on the beginning of an encoded text stream (or file). This data allows the reader to correctly decide which character encoding to use when decoding the stream back into a sequence of characters. The use of byte order markers within files and streams is not specific to XML, but its typical to see them in use when XML data is streamed or stored in files.
    https://www.liquid-technologies.com/XML/ByteOrderMarker.aspx

    The Byte Order Marker (BOM) UTF-8 BOM is a sequence of Bytes at the start of a text-stream (0xEF,0xBB,0xBF) that allows the reader to more reliably guess a file as being encoded in UTF-8.
    https://en.wikipedia.org/wiki/Byte_order_mark

    https://neodynamic.wordpress.com/2012/10/11/xmlreader-exception-on-utf-8-xml-strings-with-byte-order-mark-bom/

    if you remove this line

    System.IO.StringReader sreader = new System.IO.StringReader("simple.xml");


    to

     System.IO.TextReader sreader = new StreamReader("simple.xml");

    every things will be fine

     XmlDocument docXML = new XmlDocument() { XmlResolver = null };
              
                System.IO.TextReader sreader = new StreamReader("simple.xml");
    
              
                using (XmlReader reader = XmlReader.Create(sreader, new XmlReaderSettings() { XmlResolver = null }))
                {
                     
                    docXML.Load(reader);
    
                    Console.WriteLine(docXML.InnerXml);
    
                }

    Result


    Please remember to mark the replies as answers if they helped you :) ~


    • Edited by Rebin Qadir Saturday, March 28, 2020 6:54 PM correction text
    • Marked as answer by Duvernet Vincent Saturday, March 28, 2020 8:36 PM
    Saturday, March 28, 2020 6:52 PM
  • wonderful thanks :)

    Note :

    I had to add using () around the sreader to release file lock even after end of function call.

    Saturday, March 28, 2020 8:37 PM