Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.


XML
<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>


C#
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:

C#
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;   
       }
Posted
Updated 3-Aug-16 0:26am

1 solution

 
Share this answer
 
v2
Comments
jinesh sam 3-Aug-16 6:30am    
I refer this link.. but it was not useful for me. My XML structure is not generic

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900