简体   繁体   English

反序列化xml。 xmlns ='http // someaddress'意外

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

My problem is with deserialization of xml to c# objects. 我的问题是将xml反序列化为c#对象。 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: 在OTA_HotelResRQ中,我声明了名称空间和其他信息:

[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. 我无法为我的模型修改基类,并且我也需要具有“ ns”前缀,因为要发送到的服务需要这种格式。

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: 我尝试使用Charles提供的解决方案对其进行修复,并更新了模型:

[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: XML属性不会被继承 ,因此您需要向派生类添加XmlRoot属性:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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