我找了一个问题,但无法找到这个特殊情况的答案,所以这就是问题。
当尝试将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.)
欢迎任何帮助。
答案 0 :(得分:0)
在深入了解堆栈溢出的同时,我在以下帖子中找到了答案:
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