简体   繁体   English

XML反序列化中的特定命名空间失败

[英]Specific Namespace in XML Deserialization fails

I looked for an issue but couldn't find the answer the this particular case, so here is the issue. 我寻找了一个问题,但找不到这种特殊情况的答案,所以这里是问题。

When trying to deserialize a xml string into a strong type object I got the following message: 尝试将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()

Although the class has to my opinion the correct namespace attribute value defined, being : 尽管我认为该类定义了正确的名称空间属性值,但它是:

[XmlType(AnonymousType = true, Namespace = "urn:webbeB2B:webservices:v0")]
    public class GetPointOfDelivery_Out
    {
        [XmlElement("POD")]
        public POD[] POD
….
}

The source string looks like this: 源字符串如下所示:

<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>

I use the following code to deserialize the string 我使用以下代码反序列化字符串

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.)

Any help is welcome. 欢迎任何帮助。

While looking somewhat deeper into stack overflow I found the answer in the following post: 在深入研究堆栈溢出时,我在以下文章中找到了答案:

https://stackoverflow.com/a/1232328/1145146 https://stackoverflow.com/a/1232328/1145146

I added the XmlRoot attribute on the target class to be deserialized 我在要反序列化的目标类上添加了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.

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