none
System.InvalidOperationException: There is an error in XML document (2, 2)

    Question

  • Hello,

    I am getting an error when I tried to Deserialize the XML.

    <MYLIST>
    	<CUSTOMER_MASTER/>
    	<CUSTOMER>
    		<CUSTOMER_CODE>N11415</CUSTOMER_CODE>
    		<ACCOUNT_GROUP_CODE/>
    	</CUSTOMER>
    	<CUSTOMER>
    		<CUSTOMER_CODE>N11415</CUSTOMER_CODE>
    		<ACCOUNT_GROUP_CODE/>
    	</CUSTOMER>
    <MYLIST>

    My Class

        [XmlRoot("CUSTOMER")]
        public class Customers
        {
            [XmlElement("CUSTOMER_CODE")]
            public string CustomerCode{ get; set; }
    
            [XmlElement("ACCOUNT_GROUP_CODE")]
            public string AccountGroupCode{ get; set; 
    
    }


    Code:

       string filename = @"D:\CUSTOMERS.xml";
                    
                    XmlSerializer _xmlSerializer = new XmlSerializer(typeof(List<Customers>));
                    Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read);
                    List<Customers> custList;
                    custList = (List<Customers>)_xmlSerializer.Deserialize(stream);
                    stream.Close();


    Error :

    System.InvalidOperationException: There is an error in XML document (2, 2). ---> System.InvalidOperationException: <MYLIST xmlns=''> was not expected.
       at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderList1.Read3_ArrayOfCustomers()

    Thanks in advance.

    Best Regards

    Muthuraja

     
    Thursday, April 25, 2013 3:12 PM

Answers

All replies

  • Shouldn't

    [XmlRoot("CUSTOMER")]

    not be:

    [XmlRoot("MYLIST")]

    Cause the root element is supposed to enclose all the other elements.

    wizend


    Thursday, April 25, 2013 3:40 PM
  • Thanks for your response Wizend, but still I am getting the same error.

    Best Regards

    Muthuraja

    Thursday, April 25, 2013 4:08 PM
  • I recommend to use the XML Schema Definition (Xsd.exe) tool to generate a valid CLR class from your xml file. How to use it, you'll learn here: http://msdn.microsoft.com/en-us/library/x6c1kb0s%28v=vs.110%29.aspx

    It's simpler and you'll see in the end where your original code failed.

    But mind you, your xml has to follow the W3C standard. But this should be a minor detail.


    Thursday, April 25, 2013 4:30 PM
  • Or another approach to debug this is to serialize and see what is the XML produced by the serializer.

    IMO it could be that MYLIST wraps a *list* of customers so it seems to me it should be on a class that would inherit from List<Customers> class rather than directly on the Customers class ?


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Thursday, April 25, 2013 5:05 PM
  • Or another approach to debug this is to serialize and see what is the XML produced by the serializer.

    IMO it could be that MYLIST wraps a *list* of customers so it seems to me it should be on a class that would inherit from List<Customers> class rather than directly on the Customers class ?


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".


    This is correct.  Create a class that inherits the collection.  Add the attributes there.
    Tuesday, September 10, 2013 6:38 AM