1

Web 服务返回如下所示的文档:

<CarrierService.GetAlerts>
 <ResponseDO>
  <status>APPROVED</status>
  <action>OK</action>
  <code>SFW00542</code>
  <displayMsg/>
  <techMsg/>
 </ResponseDO>
 <AlertList count="1">
  <AlertItem>
   <docketNumber>MC758</docketNumber>
   <dotNumber>17952</dotNumber>
   <Change>
    <Field>rating</Field>
    <From>Conditional</From>
    <To>Satisfactory</To>
   </Change>
   <Change>
    <Field>rateDate</Field>
    <From>2010-08-31</From>
    <To>2010-12-20</To>
   </Change>
  </AlertItem>
 </AlertList>
</CarrierService.GetAlerts>

我正在使用 XmlSerializer,这就是我的映射类的外观:

[XmlRoot("CarrierService.GetAlerts")] 
public class GetAlertsResponse
{
    [XmlElement(ElementName = "ResponseDO")]
    public ResponseDo ResponseDo { get; set; }

    [XmlArray(ElementName = "AlertList")]
    public List<AlertItem> AlertList { get; set; }
}


public class AlertItem
{
    [XmlElement(ElementName = "docketNumber")]
    public string DocketNumber { get; set; }

    [XmlElement(ElementName = "dotNumber")]
    public string DOTNumber { get; set; }
}

我对 AlertItem.Change 有疑问。它在 AlertItem 中重复,并且没有包含在“ChangeList”中。

我该如何反序列化这些项目?

4

2 回答 2

2

您可以稍微作弊并在 List 上使用 XmlElement,XmlSerializer 将很乐意遵守。

public class AlertItem
{
    [XmlElement(ElementName = "docketNumber")]
    public string DocketNumber { get; set; }

    [XmlElement(ElementName = "dotNumber")]
    public string DOTNumber { get; set; }

    [XmlElement("Change")]
    public List<Change> Changes {get; set;}
}

public class Change
{
    [XmlElement("Field")]
    public string Field {get; set;}

    [XmlElement("From")]
    public string From {get; set;}

    [XmlElement("To")]
    public string To {get; set;}
}
于 2013-08-12T18:21:22.400 回答
0

如果您想创建更改列表,这将起作用

<CarrierService.GetAlerts>
 <ResponseDO>
  <status>APPROVED</status>
  <action>OK</action>
  <code>SFW00542</code>
  <displayMsg/>
  <techMsg/>
 </ResponseDO>
 <AlertList count="1">
  <AlertItem>
   <docketNumber>MC758</docketNumber>
   <dotNumber>17952</dotNumber>
<Changes>
   <Change>
    <Field>rating</Field>
    <From>Conditional</From>
    <To>Satisfactory</To>
   </Change>
   <Change>
    <Field>rateDate</Field>
    <From>2010-08-31</From>
    <To>2010-12-20</To>
   </Change>
</Changes>
  </AlertItem>
 </AlertList>
</CarrierService.GetAlerts>

和班级

[XmlRoot("CarrierService.GetAlerts")] 
public class GetAlertsResponse
{
    [XmlElement(ElementName = "ResponseDO")]
    public ResponseDo ResponseDo { get; set; }

    [XmlArray(ElementName = "AlertList")]
    public List<AlertItem> AlertList { get; set; }
}
public class AlertItem
{
    [XmlElement(ElementName = "docketNumber")]
    public string DocketNumber { get; set; }

    [XmlElement(ElementName = "dotNumber")]
    public string DOTNumber { get; set; }

    [XmlElement(ElementName = "Changes")]
    public Changes Changes { get; set; }
}
public class Changes
{
    [XmlElement(ElementName = "Change")]
    public  List<Change> ChangesList { get; set; }
}
public class Change
{
    [XmlElement("Field")]
    public string Field {get; set;}

    [XmlElement("From")]
    public string From {get; set;}

    [XmlElement("To")]
    public string To {get; set;}
}
于 2013-08-12T18:25:21.893 回答