简体   繁体   中英

Deserialize xml. xmlns='http//someaddress' was not expected

My problem is with deserialization of xml to c# objects. I have some class derived from some other class (I have the reason why I need to use inheritance in this place - doesn't matter why):

[Serializable]
[XmlType(TypeName = "OTA_HotelResRQ")]
public class ResRQ : OTA_HotelResRQ
{
}

in OTA_HotelResRQ I have declared namespaces and other information:

[Serializable]
[XmlTypeAttribute(Namespace = "http://www.opentravel.org/OTA/2003/05")]
[XmlRootAttribute(Namespace = "http://www.opentravel.org/OTA/2003/05", IsNullable = false)]
public class OTA_HotelResRQ : OtaRequestMessage, IRequest

And when I'm trying to serialize some request which looks like below:

<ns:OTA_HotelResRQ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:ns="http://www.opentravel.org/OTA/2003/05" 
                PrimaryLangID="en" EchoToken="5613971064477293649" ResStatus="Commit" Version="2.1">
...SOME REQUEST WITH NS:....
<ns:POS><ns:/POS>

And now when I'm trying to deserialize this I have:

There is an error in XML document (3, 2). ---> System.InvalidOperationException: <OTA_HotelResRQ xmlns='http://www.opentravel.org/OTA/2003/05'> was not expected.

do you have some idea why I can't deserialize this? I can't modify base class for my model and also I need to have "ns" prefixes because service where I want to send that requires this format.

UPDATE:

My deserialization is implemented, that I'm getting bytes from string and try to deserialize using:

return (T) new XmlSerializer(typeof (T)).Deserialize(new MemoryStream(bytes));

I tried to fix it using solution provided by Charles and I updated my model:

[Serializable]
[XmlRoot("OTA_HotelResRQ", Namespace = "http://www.opentravel.org/OTA/2003/05")]
[XmlType("OTA_HotelResRQ", Namespace = "http://www.opentravel.org/OTA/2003/05")]
public class ResRQ : OTA_HotelResRQ
{
}

but still with no success. When I try to deserialize then I'm getting exception:

Types 'OTA_HotelResRQ' and
 'ResRQ' both use the XML type name, 
'OTA_HotelResRQ', from namespace 'http://www.opentravel.org/OTA/2003/05'. Use 
XML attributes to specify a unique XML name and/or namespace for the type.

XML attributes are not inherited , so you need to add an XmlRoot attribute to your derived class:

[Serializable]
[XmlRoot("OTA_HotelResRQ", Namespace = "http://www.opentravel.org/OTA/2003/05")]
public class ResRQ : OTA_HotelResRQ
{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM