0

my xml file is

<ns2:HotelListResponse xmlns:ns2="http://v3.hotel.wsapi.ean.com/">
  <customerSessionId>0ABAAACE-8D64-3191-4732-8476D6E963C6</customerSessionId>
  <numberOfRoomsRequested>1</numberOfRoomsRequested>
  <moreResultsAvailable>true</moreResultsAvailable>
</ns2:HotelListResponse>

and this is my object type

[Serializable]
    [XmlRoot("HotelListResponse")]
    public class HotelListResponse
    {
        [XmlElement("customerSessionId")]
        public string customerSessionId { get; set; }
        [XmlElement("numberOfRoomsRequested")]
        public int numberOfRoomsRequested { get; set; }
        [XmlElement("moreResultsAvailable")]
        public bool moreResultsAvailable { get; set; }
    }

And I use this to deserlize xml

 public static object DeserializeXml(string xmlData, Type type)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(type);
            StringReader reader = new StringReader(xmlData);
            object obj = xmlSerializer.Deserialize(reader);

            return obj;
        }

But I am getting error xmlns=''> was not expected. - There is an error in XML document (2, 2)

leppie
  • 115,091
  • 17
  • 196
  • 297
vishal mane
  • 296
  • 5
  • 11
  • 1
    possible duplicate of [Error Deserializing Xml to Object - xmlns='' was not expected](http://stackoverflow.com/questions/4884383/error-deserializing-xml-to-object-xmlns-was-not-expected) – RaYell Jul 15 '14 at 06:40
  • 1
    In my case, it was because of wrong declaration of `XmlSerializer`. So check that also. – Mangesh Jun 02 '17 at 06:59

3 Answers3

2

Got it need to fix class

[Serializable, XmlRoot(ElementName = "HotelListResponse", Namespace = "http://v3.hotel.wsapi.ean.com/")]
    public class HotelListResponse
    {
        [XmlElement(ElementName = "customerSessionId", Namespace = "")]
        public string customerSessionId { get; set; }
        [XmlElement(ElementName = "numberOfRoomsRequested", Namespace = "")]
        public int numberOfRoomsRequested { get; set; }
        [XmlElement(ElementName = "moreResultsAvailable", Namespace = "")]
        public bool moreResultsAvailable { get; set; }
    }
vishal mane
  • 296
  • 5
  • 11
  • 1
    At least tell us what you did. After scanning back and forth I see you added `Namespace = ""`. Any other differences? – Quantic Oct 13 '16 at 15:35
1

Your elements have no namespace prefix, so it is expecting a default namespace.

If someone is sending you XML like that, ask them to fix it.

Edit: You may be able to explicitly specify the expected namespace with the XmlElementAttribute.

leppie
  • 115,091
  • 17
  • 196
  • 297
  • Why is no namespace on child nodes a problem? – Jay Sep 18 '17 at 19:44
  • @Jay Because they are not defined in the default namespace. See answer below for a "hacky" fix. – leppie Sep 19 '17 at 04:10
  • That explains what was done not why it's "bad". Microsoft's serialization lib assumes children have the same namespace as the parent but xml standards don't? So it's only bad practice if you're using an MS library? – Jay Sep 19 '17 at 10:56
0

Well, it's 7 years after this question was asked, and thankfully not so many people are still using XML. However, if you're using Visual Studio and you have some XML that you need to deserialize, and you can't find the right combination of attributes to decorate the classes with, you can use the 'Edit -> Paste XML as classes' option, which will create the correct set of attributes for you.

Just create a new class in VS, delete the actual class part within the namespace in the newly created file, copy your full XML to the clipboard, and then use the paste option to create the class hierarchy needed for the XML, including all of the correct XML Attributes.

This saved me a lot of trial and error with getting the correct combination of XmlRoot and XmlType attributes.

Here's the complete generated code for the example XML in the original question:

namespace Test
{

    // NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://v3.hotel.wsapi.ean.com/")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://v3.hotel.wsapi.ean.com/", IsNullable = false)]
    public partial class HotelListResponse
    {

        private string customerSessionIdField;

        private byte numberOfRoomsRequestedField;

        private bool moreResultsAvailableField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Namespace = "")]
        public string customerSessionId
        {
            get
            {
                return this.customerSessionIdField;
            }
            set
            {
                this.customerSessionIdField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Namespace = "")]
        public byte numberOfRoomsRequested
        {
            get
            {
                return this.numberOfRoomsRequestedField;
            }
            set
            {
                this.numberOfRoomsRequestedField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Namespace = "")]
        public bool moreResultsAvailable
        {
            get
            {
                return this.moreResultsAvailableField;
            }
            set
            {
                this.moreResultsAvailableField = value;
            }
        }
    }


}
Jon Knight
  • 187
  • 1
  • 11