Sri Harsha
Sri Harsha

Reputation: 51

There is an error in XML document (1, 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!

Upvotes: 1

Views: 18823

Answers (4)

NYCdotNet
NYCdotNet

Reputation: 4647

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);

Upvotes: 0

Yusril Maulidan Raji
Yusril Maulidan Raji

Reputation: 1932

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;
}

Upvotes: 1

wp78de
wp78de

Reputation: 18950

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

Upvotes: 1

Dave Black
Dave Black

Reputation: 8019

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?

Upvotes: 1

Related Questions