1

I have been trying to read data from XML. When I try to deserialize it, I get the following error:

"There is an error in XML document (1, 1)."

For your reference, I am attaching my entire code as well as my xml file.

C# Code:

CarCollection cars = null;
string path = @"C:\Users\harsha\Desktop\Doc.xml";
XmlSerializer serializer = new XmlSerializer(typeof(CarCollection));
var reader = XmlReader.Create(path);
cars = (CarCollection)serializer.Deserialize(reader);
reader.Close();

Also,the data structure is as follows:

namespace XMLDataReader
{
   [Serializable()]
   public class Car
   {
       [System.Xml.Serialization.XmlElement("StockNumber")]
       public string StockNumber { get; set; }

       [System.Xml.Serialization.XmlElement("Make")]
       public string Make { get; set; }

       [System.Xml.Serialization.XmlElement("Model")]
       public string Model { get; set; }
   }

   [Serializable()]
   [System.Xml.Serialization.XmlRoot("CarCollection")]
   public class CarCollection
   {
       [XmlArray("Cars")]
       [XmlArrayItem("Car", typeof(Car))]
       public Car[] Car { get; set; }
   }

XML File:

   <CarCollection>
<Cars>
  <Car>
    <StockNumber>1020</StockNumber>
    <Make>Nissan</Make>
    <Model>Sentra</Model>
  </Car>
  <Car>
    <StockNumber>1010</StockNumber>
    <Make>Toyota</Make>
    <Model>Corolla</Model>
  </Car>
  <Car>
    <StockNumber>1111</StockNumber>
    <Make>Honda</Make>
    <Model>Accord</Model>
  </Car>
</Cars>
</CarCollection>

Waiting for some help. Thanks in advance!

jsanalytics
  • 13,058
  • 4
  • 22
  • 43
Sri Harsha
  • 51
  • 1
  • 2
  • 7
  • Does it need the standard xml prefix (``)? – Steve Nov 14 '17 at 22:20
  • Hi Steve, I have added the line: , but still I am getting the same error.Can you suggest something else? – Sri Harsha Nov 14 '17 at 22:29
  • 1
    If something does not work in serialization then try the same action in reverse: create your object, serialize to the file and see what you get. – Optional Option Nov 14 '17 at 22:33
  • 1
    Could you please provide more output of exception? I am sure XmlSerializer provides more information on the cause, probably in Inner Exception – Dominik Szymański Nov 14 '17 at 22:37
  • Please [edit] your question to share the full `ToString()` output of the exception including the exception type, message, traceback, and **inner exception** if any. – dbc Nov 14 '17 at 23:17

4 Answers4

1

Can you open it with notepad and verify the content is what you expect? You will see that error if:

  • there is nothing in the document
  • the xml in the file is not valid or well-formed
  • the schema of the xml is different than what is specified

It could also be because you don't have the Xml Declaration header defined (depending on the version of xml spec you're using):

For example:

<?xml version="1.0" encoding="UTF-8" ?>

See Does a valid XML file require an XML declaration?

Dave Black
  • 7,305
  • 2
  • 52
  • 41
  • I just added the line above,but still I am facing the same error.What do you suggest now? – Sri Harsha Nov 14 '17 at 22:28
  • Does your process have read/write permissions to write to the path? Can you open the file in Internet Explorer? IE has a built-in validation mechanism that sometimes helps. – Dave Black Nov 14 '17 at 22:34
  • Hi Dave, I tried to open it using IE,But I could not see the content there? How can I make it accessible to IE then? I guess,my code will also automatically run,if IE is able to see the content,right? – Sri Harsha Nov 14 '17 at 22:38
  • can you open it with notepad? Sounds like maybe the file is corrupt. If there is nothing in the document, you will see that error. – Dave Black Nov 14 '17 at 22:41
  • Hi Dave,its opening with notepad,but the content ,when viewed with notepad is completely different :(.. – Sri Harsha Nov 14 '17 at 22:42
  • 2
    there you go. The file is corrupt (or different than what is expected). The serialization engine cannot make sense of the structure in the document. – Dave Black Nov 14 '17 at 22:43
  • 1
    What do you mean by "is completely different"? Maybe that's what means "corrupted". XML isn't some special-case format, should be correctly viewable in ordinary notepad. – Dominik Szymański Nov 14 '17 at 22:44
1

Your code works fine (with and without the XML declaration), the problem is most probably the content of your XML file. I guess the encoding is broken or there are some hidden special characters that look like regular characters but are something else (e.g. the < sign).

enter image description here

wp78de
  • 18,207
  • 7
  • 43
  • 71
  • I copied and saved your sample XML with Notepad++ 'as is'. – wp78de Nov 14 '17 at 22:59
  • 1
    I am able to replicate this result as well. Code successfully de-serialized as provided. – Tyler Lee Nov 14 '17 at 22:59
  • Hi Lee,thanks a lot for your reply. I have another xml file,which is formatted properly and is also viewed by IE.But,now,I am getting an error "There is an error in XML document (1, 2)." What could be the reason in this case? – Sri Harsha Nov 14 '17 at 23:06
  • I don't get it. Here is my working sample project https://www.file-upload.com/57aom9qgz3sk – wp78de Nov 14 '17 at 23:21
1

In my case, somehow, there is a bug in the serializer. I am not sure whether it's the library or the logic.

To check whether you have the same case like me:

  • Copy your XML from your database column,
  • Open a notepad,
  • Paste the xml into the notepad,
  • Save

It will tell you that there is something wrong with it: enter image description here

If you continue to press Ok, then open the file again. There will be a question mark there: enter image description here

To solve this, before deserializing the xml string, I simply check whether the first character is an angel bracket or not. If not, then remove that character:

private static string Validate(string xml)
{
    if (string.IsNullOrEmpty(xml))
        return xml;

    try
    {
        var index = xml.IndexOf('<');
        if (index > 0)
            xml = xml.Substring(index);
    }
    catch { }

    return xml;
}
Yusril Maulidan Raji
  • 1,682
  • 1
  • 21
  • 46
0

The cause for the 1,1 character being an error is likely the UTF-8 byte order mark. You can avoid this problem by encoding the XML without a BOM (though this may cause other systems to not process the file correctly - be aware of if having a BOM is in your requirements), or by passing a stream reader that interprets the BOM correctly to the XmlReader.Create method.

using var fileReader = new StreamReader(path, true);  // true means to detect the BOM.
var reader = XmlReader.Create(fileReader);

NYCdotNet
  • 4,500
  • 1
  • 25
  • 27