使用IXmlSerializable反序列化集合时出错

时间:2013-11-20 15:58:27

标签: c# xml

我尝试使用IXmlSerializable接口反序列化集合。我在行reader.ReadStartElement(“IncomeList”)中得到错误。错误消息是“'endelement'是无效的xmlnodetype”。为什么我会收到此错误?我该怎么办? xml文件的结构是

<?xml version="1.0"?>
<ArrayOfBusinessOffer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <BusinessOffer>
    <State>InWork</State>
    <CostsList count="1">
      <Costs>
        <Type>Optional</Type>
        <Amount>55</Amount>
        <Details count="1">
          <CostDetail>
            <SubType>sub</SubType>
            <Amount>55</Amount>
            <IsRefused>True</IsRefused>
          </CostDetail>
        </Details>
      </Costs>
    </CostsList>
    <IncomeList incomeListCount="1">
      <Income>
        <Amount>789</Amount>
        <IncomeSource>
          <ID>55</ID>
          <Name>IncomeSource</Name>
          <IsSystem>True</IsSystem>
        </IncomeSource>
      </Income>
    </IncomeList>
    <ActualAge>45</ActualAge>
    <RetireAge>33</RetireAge>
    <DeathAge>80</DeathAge>
    <AccumulateAmount>23</AccumulateAmount>
    <Percent>25</Percent>
    <TotalAmount>25</TotalAmount>
  </BusinessOffer>
  <BusinessOffer>
    <State>InWork</State>
    <CostsList count="1">
      <Costs>
        <Type>Optional</Type>
        <Amount>55</Amount>
        <Details count="1">
          <CostDetail>
            <SubType>sub</SubType>
            <Amount>55</Amount>
            <IsRefused>True</IsRefused>
          </CostDetail>
        </Details>
      </Costs>
    </CostsList>
    <IncomeList incomeListCount="1">
      <Income>
        <Amount>789</Amount>
        <IncomeSource>
          <ID>55</ID>
          <Name>IncomeSource</Name>
          <IsSystem>True</IsSystem>
        </IncomeSource>
      </Income>
    </IncomeList>
    <ActualAge>45</ActualAge>
    <RetireAge>33</RetireAge>
    <DeathAge>80</DeathAge>
    <AccumulateAmount>23</AccumulateAmount>
    <Percent>25</Percent>
    <TotalAmount>25</TotalAmount>
  </BusinessOffer>
</ArrayOfBusinessOffer> 

源代码:

public void ReadXml(XmlReader reader)
                {
                    reader.MoveToContent();
                    reader.ReadStartElement();
                    State = (OfferState)Enum.Parse(typeof(OfferState),reader.ReadElementString("State"));
                    int count = int.Parse(reader.GetAttribute("count"));
                    reader.ReadStartElement("CostsList");    
                    XmlSerializer serializer = new XmlSerializer(typeof(Costs));
                    for (int i = 0; i < count; i++)
                    {
                        Costs item = (Costs) serializer.Deserialize(reader);
                        CostsList.Add(item);
                    }

                    reader.ReadEndElement();
                    reader.ReadEndElement();

                    reader.ReadStartElement("IncomeList");
                    string str = reader["incomeListCount"];
                    serializer = new XmlSerializer(typeof(Income));
                    for (int i = 0; i < count; i++)
                    {
                        Income item = (Income)serializer.Deserialize(reader);
                        IncomeList.Add(item);
                    }

                    ActualAge = int.Parse(reader.ReadElementString("ActualAge"));
                    RetireAge = int.Parse(reader.ReadElementString("RetireAge"));
                    DeathAge = int.Parse(reader.ReadElementString("DeathAge"));
                    AccumulateAmount = int.Parse(reader.ReadElementString("AccumulateAmount"));
                    Percent = int.Parse(reader.ReadElementString("Percent"));       
                    TotalAmount = int.Parse(reader.ReadElementString("TotalAmount"));;
                }

0 个答案:

没有答案