Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am getting the response from an API as
XML
<Items To="3" HasMore="false" From="0">
<VCNAuthItem VCardAlias="NT-AED" UserName="SAM" TxnType="0" TxnId="3792664853" SystemDate="2016-02-02 16:32:31.00 +0000" Status="A" Settled="U" RespCode="51" PurchaseId="44926647" MerchantName="TRUMP INTERNATIONAL HO NEW YORK NY " ExpiryDate="1604" EffectOnCpnAmount="0" Description="Cumul limit fail" CurrencyCodeTransaction="840" CpnPan="0000000000" CorporateName="NTT-VA" CardAcceptorId="000067070700018" AmountTransaction="8000" AcquirerId="007660"/>

<VCNAuthItem VCardAlias="NT-AED" UserName="SAM" TxnType="0" TxnId="3792649391" SystemDate="2016-02-02 15:34:51.00 +0000" Status="A" Settled="U" RespCode="05" PurchaseId="44927835" MerchantName="PAYPAL 4029357733 LUX" ExpiryDate="1604" EffectOnCpnAmount="0" Description="MCC Limit Fail" CurrencyCodeTransaction="840" CpnPan="00000000" CorporateName="NTT-VA" CardAcceptorId="000980020014992" AmountTransaction="1" AcquirerId="011407"/>

<VCNAuthItem VCardAlias="NT-AED" UserName="SAM" TxnType="0" TxnId="3792647261" SystemDate="2016-02-02 15:26:01.00 +0000" Status="A" Settled="U" RespCode="05" PurchaseId="24822154" MerchantName="CAMPAIGNER BY PROTUS 888-845-4544 CA " ExpiryDate="1604" EffectOnCpnAmount="0" Description="Iss/Netwk decline" CurrencyCodeTransaction="840" CpnPan="00000000" CorporateName="NTT-VA" CardAcceptorId="013108000152040" AmountTransaction="65" AcquirerId="003286"/>
</Items>

How can i loop through the all items and get the values

Thanks and Regards
RK

What I have tried:

I have tried to loop through the XML like
C#
foreach (XmlNode node in xDoc.SelectNodes("/Items"))
{
}
Posted
Updated 14-Mar-16 3:32am
v3
Comments
Peter Leow 14-Mar-16 8:57am    
Stop re-posting.

Use Xml.Linq to get required values. Try with below code:
C#
string temp = @"<items to="3" hasmore="false" from="0">
<vcnauthitem vcardalias="NT-AED" username="SAM" txntype="0" txnid="3792664853" systemdate="2016-02-02 16:32:31.00 +0000" status="A" settled="U" respcode="51" purchaseid="44926647" merchantname="TRUMP INTERNATIONAL HO NEW YORK NY " expirydate="1604" effectoncpnamount="0" description="Cumul limit fail" currencycodetransaction="840" cpnpan="0000000000" corporatename="NTT-VA" cardacceptorid="000067070700018" amounttransaction="8000" acquirerid="007660" />
<vcnauthitem vcardalias="NT-AED" username="SAM" txntype="0" txnid="3792649391" systemdate="2016-02-02 15:34:51.00 +0000" status="A" settled="U" respcode="05" purchaseid="44927835" merchantname="PAYPAL 4029357733 LUX" expirydate="1604" effectoncpnamount="0" description="MCC Limit Fail" currencycodetransaction="840" cpnpan="00000000" corporatename="NTT-VA" cardacceptorid="000980020014992" amounttransaction="1" acquirerid="011407" />
</items>";

XDocument doc = XDocument.Parse(temp);
var tempobj = (from rec in doc.Descendants("VCNAuthItem")
			  select new
				  {
					  TxnId = rec.Attribute("TxnId").Value,
					  SystemDate = rec.Attribute("SystemDate").Value,
					  PurchaseId = rec.Attribute("PurchaseId").Value,
					  Description = rec.Attribute("Description").Value,
					  CardAcceptorId = rec.Attribute("CardAcceptorId").Value
				  }).ToList();

foreach (var p in tempobj)
{
	string tempo = p.TxnId;
}

Note: You may need to add more attribute names in above Linq as per your need.
 
Share this answer
 
C#
foreach (XmlNode node in xDoc.SelectNodes("/Items"))
{
    foreach (XmlNode itemNode in node.SelectNodes("VCNAuthItem"))
    {
        string alias = itemNode.Attributes["VCardAlias"].InnerText;
                    
    }
}
 
Share this answer
 

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