简体   繁体   English

使用XmlSerializer并设置根类名称

[英]Using XmlSerializer and setting root class name

Trying to work with XmlSerializer to nicely deserialize stuff I get from webservice. 尝试与XmlSerializer一起很好地反序列化从Web服务获得的内容。

Here is my class declaration: 这是我的课堂宣言:

[Serializable]
    public class CarrierLookupResponse
    {
        [XmlElement(ElementName = "ResponseDO")]
        public ResponseDo ResponseDo { get; set; }
    }

Here is how XML looks: 这是XML的外观:

<?xml version="1.0" encoding="utf-8" ?>
<CarrierService.CarrierLookup>
  <ResponseDO>
    <status>APPROVED</status>
    <action>OK</action>
    <code>SFW00389</code>
    <displayMsg></displayMsg>
    <techMsg></techMsg>
  </ResponseDO>

Here is code I use to deserialize: 这是我用来反序列化的代码:

var serializer = new XmlSerializer(typeof(CarrierLookupResponse));
            var carrierLookupResponse = serializer.Deserialize(new StringReader(response.Key)) as CarrierLookupResponse;

Problem is simple. 问题很简单。 Service returns "CarrierService.CarrierLookup" and I need to force it to deserialize into "CarrierLookupResponse" 服务返回“ CarrierService.CarrierLookup”,我需要强制将其反序列化为“ CarrierLookupResponse”

I can't put XmlElement attribute on class itself, so I have no idea how to map this name properly. 我不能将XmlElement属性放在类本身上,所以我不知道如何正确映射此名称。

Have you tried with XmlRoot attribute? 您是否尝试过使用XmlRoot属性?

    [Serializable]
    [XmlRoot("CarrierService.CarrierLookup")] 
    public class CarrierLookupResponse
    {
    ...

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

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