cancel
Showing results for 
Search instead for 
Did you mean: 

c# There is an error in XML document .

Highlighted
New Contributor

c# There is an error in XML document .

I checked several other answers that refer to checking if there is an extranneous byte in the front of the XML, that isn't the case. I isolated my XML deserialization code to highlight the issue. I believe that the XML is properly decorated (it's an AccessControlPolicy object from Amazon S3). When I try to deserialize it, I get the aforementioned error.

Here is the isolated code showing the classes, XML decoration, deserialization, and output.

 

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Text; 
using System.Xml.Serialization; 

namespace sandbox
{
    public partial class Program
    {
        static void Main(string[] args)
        {
            string xml = "<AccessControlPolicy xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><Owner><ID>946a0786-3840-4007-afe1-76f138a3d31c</ID></Owner><AccessControlList></AccessControlList></AccessControlPolicy>";

            try
            {
                AccessControlPolicy acp = DeserializeXml<AccessControlPolicy>(xml);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                Console.WriteLine(e.Message); 
            }

            Console.ReadLine();
        }

        public static T DeserializeXml<T>(string xml)
        { 
            XmlSerializer xmls = new XmlSerializer(typeof(T));
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
            {
                return (T)xmls.Deserialize(ms);
            } 
        }
    }

    [XmlRoot(ElementName = "AccessControlPolicy", Namespace = "http://s3.amazonaws.com/doc/2006-03-01")]
    public class AccessControlPolicy
    {
        [XmlElement(ElementName = "Owner", Namespace = "http://s3.amazonaws.com/doc/2006-03-01")]
        public Owner Owner { get; set; }
        [XmlElement(ElementName = "AccessControlList", Namespace = "http://s3.amazonaws.com/doc/2006-03-01")]
        public AccessControlList AccessControlList { get; set; }
    }

    [XmlRoot(ElementName = "AccessControlList", Namespace = "http://s3.amazonaws.com/doc/2006-03-01", IsNullable = true)]
    public class AccessControlList
    {
        [XmlElement(ElementName = "Grant", Namespace = "http://s3.amazonaws.com/doc/2006-03-01", IsNullable = true)]
        public List<Grant> Grant { get; set; }
    }

    [XmlRoot(ElementName = "Grant", Namespace = "http://s3.amazonaws.com/doc/2006-03-01")]
    public class Grant
    {
        [XmlElement(ElementName = "Grantee", Namespace = "http://s3.amazonaws.com/doc/2006-03-01")]
        public Grantee Grantee { get; set; }
        [XmlElement(ElementName = "Permission", Namespace = "http://s3.amazonaws.com/doc/2006-03-01")]
        public string Permission { get; set; }
    }

    [XmlRoot(ElementName = "Grantee", Namespace = "http://s3.amazonaws.com/doc/2006-03-01")]
    public class Grantee
    {
        [XmlElement(ElementName = "ID", Namespace = "http://s3.amazonaws.com/doc/2006-03-01")]
        public string ID { get; set; }
        [XmlElement(ElementName = "URI", Namespace = "http://s3.amazonaws.com/doc/2006-03-01")]
        public string URI { get; set; }
        [XmlElement(ElementName = "DisplayName", Namespace = "http://s3.amazonaws.com/doc/2006-03-01")]
        public string DisplayName { get; set; }
        [XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Xsi { get; set; }
        [XmlAttribute(AttributeName = "type", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
        public string Type { get; set; }
    }

    [XmlRoot(ElementName = "Owner", Namespace = "http://s3.amazonaws.com/doc/2006-03-01")]
    public class Owner
    {
        [XmlElement(ElementName = "ID", Namespace = "http://s3.amazonaws.com/doc/2006-03-01")]
        public string ID { get; set; }
        [XmlElement(ElementName = "DisplayName", Namespace = "http://s3.amazonaws.com/doc/2006-03-01")]
        public string DisplayName { get; set; }
    }
}

And the output:

 

System.InvalidOperationException: There is an error in XML document (1, 2). ---> System.InvalidOperationException: <AccessControlPolicy xmlns='http://s3.amazonaws.com/doc/2006-03-01/'> was not expected.
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderAccessControlPolicy.Read7_AccessControlPolicy()
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
   at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
   at sandbox.Program.DeserializeXml[T](String xml) in C:\code\misc\sandbox\sandbox\Program.cs:line 33

For reference, the pretty-formatted XML:

 

<AccessControlPolicy xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
   <Owner>
      <ID>946a0786-3840-4007-afe1-76f138a3d31c</ID>
   </Owner>
   <AccessControlList />
</AccessControlPolicy>

Also, prepending with <?xml version="1.0" encoding="UTF-8"?> does not seem to relieve the issue.

New Here?
Join us and watch the welcome video:
Join the September Hub-bub to show off, learn and win