[英]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.