[英]Specific Namespace in XML Deserialization fails
我寻找了一个问题,但找不到这种特殊情况的答案,所以这里是问题。
尝试将xml字符串反序列化为强类型对象时,出现以下消息:
System.InvalidOperationException: There is an error in XML document (1, 2). ---> System.InvalidOperationException: <GetPointOfDelivery_Out xmlns='urn:webbeB2B:webservices:v0'> was not expected.
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderGetPointOfDelivery_Out.Read16_GetPointOfDelivery_Out()
尽管我认为该类定义了正确的名称空间属性值,但它是:
[XmlType(AnonymousType = true, Namespace = "urn:webbeB2B:webservices:v0")]
public class GetPointOfDelivery_Out
{
[XmlElement("POD")]
public POD[] POD
….
}
源字符串如下所示:
<out:GetPointOfDelivery_Out xmlns:out="urn:webbeB2B:webservices:v0">
<out:POD>
<out:PODID>FT}UntwKNFlX0000h100Dm</out:PODID>
….
</out:POD>
<out:ErrorMessage>
<out:MsgType>S</out:MsgType>
</out:ErrorMessage>
</out:GetPointOfDelivery_Out>
我使用以下代码反序列化字符串
var xmlSerializer = new XmlSerializer(typeof(T));
using (var textReader = new StringReader(xml))
{
return (T)xmlSerializer.Deserialize(textReader);
}
(Where T is GetPointOfDelivery_Out and xml the example given above.)
欢迎任何帮助。
在深入研究堆栈溢出时,我在以下文章中找到了答案:
https://stackoverflow.com/a/1232328/1145146
我在要反序列化的目标类上添加了XmlRoot属性
[Serializable]
[XmlRoot("GetPointOfDelivery_Out", Namespace = "urn:webbeB2B:webservices:v0")]
[XmlType(AnonymousType = true, Namespace = "urn:webbeB2B:webservices:v0")]
public class GetPointOfDelivery_Out
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.