如何使用索引号反序列化哪些节点重复出现的XML [英] How to deserialize XML which nodes are recurring with index number

查看:77
本文介绍了如何使用索引号反序列化哪些节点重复出现的XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



我需要反序列化xml。由于xml节点命名约定不是标准的,我无法在构建的serialize和deserialize方法中使用.net。每个节点名称都以Item1,Item2等形式出现......请帮助实现此目的。我尝试使用Xdoc Descendants但没有清楚地了解如何继续。





Hi Friends,

I need to deserialize a xml. since the xml node naming convention is not standard i am not able to use the .net in built serialize and deserialize method. Each node name is coming as Item1, Item2 etc... Please help to achieve this. I tried with using Xdoc Descendants but not getting clear picture how to proceed.


<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; }
}





What I have tried:





What I have tried:

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;   
       }

推荐答案

XML Serialization and Deserialization: Part-1[^]





C#.Net – XML DeSerialization in C# - C# Tutorials[^]


这篇关于如何使用索引号反序列化哪些节点重复出现的XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆