Xml反序列化器无法反序列化

时间:2014-01-24 21:48:42

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

我想将xml文件反序列化为一个对象,但无法从下面的xml获取上下文属性到该对象。

<Receipt>
    <Data context="LastYear">
      <OtherIncomeReceiptDataEntity  ID="5011">
        <Amount>5000</Amount>
        <DatePaid>41274</DatePaid>
        <TaxDeducted>1000</TaxDeducted>
        <AllowableExpenses>2000</AllowableExpenses>
        <Status>Ok</Status>
      </OtherIncomeReceiptDataEntity>
    </Data>
    <Data context="CurrentYear">
      <OtherIncomeReceiptDataEntity  ID="5012">
        <Amount>5000</Amount>
        <DatePaid>41274</DatePaid>
        <TaxDeducted>1000</TaxDeducted>
        <AllowableExpenses>2000</AllowableExpenses>
        <Status>Ok</Status>
      </OtherIncomeReceiptDataEntity>
    </Data>
</Receipt>

类对象

public class Receipt
{
    public List<OtherIncomeReceiptDataEntity> Data { get; set; }
}

public class OtherIncomeReceiptDataEntity
{
    [XmlAttribute("openspaceID")]
    public int openspaceID { get; set; }

    public double Amount { get; set; }
    public int DatePaid { get; set; }
    public double TaxDeducted { get; set; }
    public double AllowableExpenses { get; set; }
    public DBEStatus Status { get; set; }
}

生成反序列化的代码

    Receipt otherIncomes = null;
    string path = "otherIncomes.xml";

    XmlSerializer serializer = new XmlSerializer(typeof(Receipt));
    StreamReader reader = new StreamReader(path);

    otherIncomes = (Receipt)serializer.Deserialize(reader);         
    reader.Close();

任何建议都将不胜感激!

更新(对象结构)

public class Receipt
{
    public List<Data> Data { get; set; }
}

public class Data
{
    [XmlAttribute("context")]
    public int context { get; set; }

    public OtherIncomeReceiptDataEntity OtherIncomeReceiptDataEntity { get; set; } 
}

0 个答案:

没有答案