Как десериализовать XML, какие узлы повторяются с индексным номером
Привет Друзья,
Мне нужно, чтобы десериализовать XML-файле. поскольку узел XML правила именования не является стандартным я не в состоянии использовать .объем встроенной сериализации и десериализации метод. Каждое имя узла приходит как Item1, Item2 и т. д... Пожалуйста, помогите достичь этого. Я попробовал использовать потомков Xdoc, но не получил четкой картины, как действовать дальше.
<YearlyContributions> <Item1> <contribution> <ContributionId>4</ContributionId> <CType>profitsharing</CType> <CSubType>Amount</CSubType> <TaxTreatment>PreTax</TaxTreatment> <Account> <AccountID>5</AccountID> <Type>401K</Type> <LimitGrp>401K</LimitGrp> <AcClient> <birthyear>1971</birthyear> <ClientID>2</ClientID> <endOfAnalysisYear>2066</endOfAnalysisYear> <Isprimary>N</Isprimary> <retirementYear>2040</retirementYear> </AcClient> <MarketValue>30000</MarketValue> <VAAccountType/> </Account> <income> <IncomeID>2</IncomeID> <Type>SB</Type> <Client> <birthyear>1971</birthyear> <ClientID>2</ClientID> <endOfAnalysisYear>2066</endOfAnalysisYear> <Isprimary>N</Isprimary> <retirementYear>2040</retirementYear> </Client> <OwnershipType>Single</OwnershipType> <TaxExempt>N</TaxExempt> <CertainGauranteePeriod>0</CertainGauranteePeriod> <SurvivorshipPercent>0</SurvivorshipPercent> <exclusionratio>0</exclusionratio> <ExclusionRatioExpectedDeathAge>0</ExclusionRatioExpectedDeathAge> <AmountPeriodID>4</AmountPeriodID> </income> <AmountPeriodID>0</AmountPeriodID> </contribution> <stateofmortality/> <YearlyAmts> <Item1> <year>2016</year> <netAmount>50000</netAmount> </Item1> <Item2> <year>2017</year> <netAmount>51000</netAmount> </Item2> <Item3> <year>2018</year> <netAmount>52020</netAmount> </Item3> <Item4> <year>2019</year> <netAmount>53060.4</netAmount> </Item4> <Item5> <year>2020</year> <netAmount>54121.608</netAmount> </Item5> </YearlyAmts> </Item1> <Item2> <contribution> <ContributionId>5</ContributionId> <CType>profitsharing</CType> <CSubType>Percent</CSubType> <TaxTreatment>PreTax</TaxTreatment> <Account> <AccountID>9</AccountID> <Type>VA</Type> <LimitGrp>401K</LimitGrp> <AcClient> <birthyear>1970</birthyear> <ClientID>1</ClientID> <endOfAnalysisYear>2065</endOfAnalysisYear> <Isprimary>Y</Isprimary> <retirementYear>2039</retirementYear> </AcClient> <MarketValue>30000</MarketValue> <VAAccountType>401K</VAAccountType> </Account> <income> <IncomeID>1</IncomeID> <Type>SB</Type> <Client> <birthyear>1970</birthyear> <ClientID>1</ClientID> <endOfAnalysisYear>2065</endOfAnalysisYear> <Isprimary>Y</Isprimary> <retirementYear>2039</retirementYear> </Client> <OwnershipType>Single</OwnershipType> <TaxExempt>N</TaxExempt> <CertainGauranteePeriod>0</CertainGauranteePeriod> <SurvivorshipPercent>0</SurvivorshipPercent> <exclusionratio>0</exclusionratio> <ExclusionRatioExpectedDeathAge>0</ExclusionRatioExpectedDeathAge> <AmountPeriodID>3</AmountPeriodID> </income> <AmountPeriodID>0</AmountPeriodID> </contribution> <stateofmortality/> <YearlyAmts> <Item25> <year>2016</year> <netAmount>40000</netAmount> </Item25> <Item26> <year>2017</year> <netAmount>40800</netAmount> </Item26> <Item27> <year>2018</year> <netAmount>41616</netAmount> </Item27> <Item28> <year>2019</year> <netAmount>42448.32</netAmount> </Item28> <Item29> <year>2020</year> <netAmount>43297.2864</netAmount> </Item29> <Item30> <year>2021</year> <netAmount>44163.232128</netAmount> </Item30> </YearlyAmts> </YearlyContributions>
public class YearlyContributions { public List<Item> Item { get; set; } } public class Item { public Contribution Contribution { get; set; } public string stateofmortality { get; set; } public List<YearlyAmt> YearlyAmts { get; set; } } public class Contribution { public int ContributionId { get; set; } public string CType { get; set; } public string CSubType { get; set; } public string TaxTreatment { get; set; } public Account Account { get; set; } public Income Income { get; set; } public int AmountPeriodID { get; set; } } public class Account { public int AccountID { get; set; } public string Type { get; set; } public string LimitGrp { get; set; } public AcClient AcClient { get; set; } public double MarketValue { get; set; } public string VAAccountType { get; set; } } public class AcClient { public int birthyear { get; set; } public int ClientID { get; set; } public int endOfAnalysisYear { get; set; } public string Isprimary { get; set; } public int retirementYear { get; set; } } public class Income { public int IncomeID { get; set; } public string Type { get; set; } public AcClient Client { get; set; } public string OwnershipType { get; set; } public string TaxExempt { get; set; } public double CertainGauranteePeriod { get; set; } public double SurvivorshipPercent { get; set; } public double exclusionratio { get; set; } public double ExclusionRatioExpectedDeathAge { get; set; } public int AmountPeriodID { get; set; } } public class YearlyAmt { public List<YearlyItem> YearlyAmts { get; set; } } public class YearlyItem { public int year { get; set; } public double netAmount { get; set; } }
Что я уже пробовал:
public static YearlyContributions DeserializeYearlyContribution(string xmlpath) { YearlyContributions yc = new YearlyContributions(); string path = string.Concat(xmlpath, @"\YearlyContributionsInputs.xml"); XDocument doc = XDocument.Load(path); var res = doc.Descendants("YearlyAmts") .Select(s => new YearlyAmt { }); return yc; }