首页 > 解决方案 > XML 反序列化为嵌套的对象数组。再次

问题描述

我正在从 Web 服务获取一些 XML。我建立了一些类来反序列化它。它部分工作;AdvMortgage我得到了对象原始成员的预期值。

Mortgage正如代码所示,我得到了any 字段的空值AdvMortgage。这与我最近发布的问题类似,但我确实遇到了麻烦。我究竟做错了什么?

    <Product xmlns="http://schemas.datacontract.org/2004/07/mInitechService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
       ... 
      <AdvMortgage>
        <Mortage>
          <MortgageType>...</MortgageType>
          <MortgageType>...</MortgageType>
          <MortgageType>
            <apr>4.99</apr>
            <discountPts>0.00</discountPts>
            <originationFee>0.00</originationFee>
            <pgmName>7/1 Adjustable</pgmName>
            <rate>4.875</rate>
          </MortgageType>
        </Mortage>
        <mortEffectiveDate>Rates effective 12/4/2018</mortEffectiveDate>
        <mortNote>
          ARM = Adjustable Rate Mortgage
          
          ...
        </mortNote>
      </AdvMortgage>
      ...
      <effectiveDate>Rates effective from 12/01/2018 through 12/31/2018</effectiveDate>
    </Product>

和课程:

namespace InitechServiceEntities
{
    [XmlType(AnonymousType = true, Namespace = "http://schemas.datacontract.org/2004/07/mInitechService")]
    [XmlRoot(Namespace = "http://schemas.datacontract.org/2004/07/mInitechService", IsNullable = false)]
    public class Product
    {
        //...
        [XmlElement("AdvMortgage")]
        public AdvMortgage AdvMortgage;
        [XmlElement("effectiveDate")]
        public string effectiveDate;
    }

    [XmlType("AdvMortgage")]
    public class AdvMortgage
    {
        [XmlElement("mortEffectiveDate")]
        public string mortEffectiveDate;
        [XmlElement("mortNote")]
        public string mortNote;
        [XmlArray(ElementName = "Mortgage")]
        [XmlArrayItem(Type = typeof(MortgageType))]
        public MortgageType[] Mortgage;
    }

    [XmlType("MortgageType")]
    public class MortgageType
    {
        [XmlElement("pgmName")]
        public string pgmName;
        [XmlElement("rate")]
        public string rate;
        [XmlElement("apr")]
        public string apr;
        [XmlElement("discountPts")]
        public string discountPts;
        [XmlElement("originationFee")]
        public string originationFee;
    }
}

标签: c#xmldeserialization

解决方案


grek40,er-shoaib,谢谢。我一直盯着它看了那么久,我从来没有意识到写服务的人拼错了“抵押贷款”。我需要休假。


推荐阅读