【问题标题】:XML Deserialization not working past the first elementXML反序列化在第一个元素之后不起作用
【发布时间】:2019-05-11 21:22:10
【问题描述】:

我正在尝试使用 XML 反序列化将来自主机的 XML 响应转换为 C# 对象。根元素被转换为它的对象,第二个元素似乎被转换,但无论如何它是一个空元素。除此之外,没有其他元素被转换。我错过了什么?

我已经为每个元素和元素数组尝试了许多不同的对象变体(即使不需要数组。我什至发现您可以将 XML 粘贴到 Visual Studio 的编辑器中,它将创建序列化对象(编辑,特殊粘贴,将 XML 粘贴为类),但我无法得到任何工作!

以下是 XML 的外观:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE paymentService PUBLIC "-//MySite/DTD MySite PaymentService v1//EN" "http://dtd.mysite.com/paymentService_v1.dtd">
<paymentService version="1.4" merchantCode="ExampleCode1">
    <reply>
        <orderStatus orderCode="ExampleOrder1">
            <reference id="YourReference">https://payments-test.mysite.com/app/hpp/integration/wpg/corporate?OrderKey=NGPPTESTMERCH1%5Ejsxml3835829684&amp;Ticket=00146321872957902pqKhCTUf0vajKCw-X5HqZA</reference>
        </orderStatus>
    </reply>
</paymentService>

-或-

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE paymentService PUBLIC "-//MySite//DTD MySite PaymentService v1//EN" "http://dtd.MySite.com/paymentService_v1.dtd">
<paymentService version="1.4" merchantCode="ExampleCode1">
   <reply>
      <orderStatus orderCode="ExampleOrder1">
         <error code="2">
            <![CDATA[Invalid address: Postal code is missing or empty.]]>
        </error>
      </orderStatus>
   </reply>
</paymentService>

这里是对象:

   [Serializable]
   [XmlRoot(ElementName = "paymentService")]
   public partial class PaymentResponse
   {
      [XmlAttribute()]
      public string version { get; set; }

      [XmlAttribute()]
      public string merchantCode { get; set; }

      [XmlElement("reply")]
      public Reply reply { get; set; }
   }
   [Serializable]
   public partial class Reply
   {
      [XmlElement("orderStatus")]
      public OrderStatus orderStatus {get; set; }
   }
   [Serializable]
   public partial class OrderStatus
   {
         [XmlAttribute()]
         public string orderCode {get; set; }

         [XmlElement(ElementName = "reference",IsNullable =true)]
         public Reference reference {get; set; }

         [XmlElement(ElementName = "error",IsNullable =true)]
         public Error error {get; set; }
   }

这是进行反序列化的调用:

XmlSerializer serializer = new XmlSerializer(typeof(PaymentResponse));
        PaymentResponse response = (PaymentResponse)serializer.Deserialize(reader);

PaymentResponse 中商家代码和版本中返回的唯一内容。

【问题讨论】:

    标签: c# .net xml-serialization xml-deserialization


    【解决方案1】:

    您可能必须告诉序列化程序如何处理您的非原始类型。

    尝试更改此设置

    [XmlElement(ElementName = "reference",IsNullable =true)]
    public Reference reference {get; set; }
    
    [XmlElement(ElementName = "error",IsNullable =true)]
    public Error error {get; set; }
    

    到这里

    [XmlElement(ElementName = "reference", Type = typeof(Reference), IsNullable = true)]
    public Reference reference {get; set; }
    
    [XmlElement(ElementName = "error", Type = typeof(Error), IsNullable = true)]
    public Error error {get; set; }
    

    祝你好运!

    【讨论】:

      猜你喜欢
      • 2011-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-02
      • 1970-01-01
      相关资源
      最近更新 更多