Click here to Skip to main content
15,885,910 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to get two nodes values

XML

XML
<Customers>
<quote id="1">
     <service>
       <name>Test value 1</name>
     </service>
     <rate>
       <amount>9510</amount>
     </rate>
   </quote>
   <quote id="2">
     <service>
       <name>Test value 2</name>
     </service>
     <rate>
       <amount>4515</amount>
     </rate>
</quote>
</Customers>


I have to try

C# code

C#
string names="";
 
  XmlDocument doc = new XmlDocument();

        doc.LoadXml(result);

        XmlNodeList xnList = doc.SelectNodes("//Customers/quote/service");
       
        foreach (XmlElement xn in xnList)
        { 
            string NN = xn["name"].InnerText;
            if (NN != "")
            {
                names += NN + ",";                 
            }
        }
Response.write(names);


I got Result :

Test value 1,Test value 2


But i want to get result like this

Test value 1@9510,Test value 2@4515


Plese help me

Thanks in advance
Posted
Updated 7-May-14 5:12am
v2

C#
var document = new XmlDocument();
document.LoadXml(result);

var nodes = document.SelectNodes("//Customers/quote");

string output = String.Empty;
for (int count = 0; count < nodes.Count; count++)
{
    var quote = nodes[count];
    if (quote != null)
    {
        var nameNode = quote.SelectNodes("service/name");
        if (nameNode != null)
        {
            output += nameNode[0].InnerText;
        }

        var amountNode = quote.SelectNodes("rate/amount");
        if (amountNode != null)
        {
            output = string.Format("{0}@{1}", output, amountNode[0].InnerText);
        }
    }

    output = string.Format("{0}, ", output);
}
 
Share this answer
 
Comments
CHill60 7-May-14 11:30am    
Oops - apologies for the overlap. Nicer formatting of the output in your solution :-)
And better defensive checking for null!!
Manas Bhardwaj 7-May-14 11:33am    
I am using ReSharper as a Paired Programmer :), so it keeps me bugging down to check for nulls.
Murugesan22 7-May-14 11:33am    
Thanks.. It will work for me
Manas Bhardwaj 7-May-14 11:34am    
glad it helped!
Maciej Los 7-May-14 16:22pm    
+5!
You just need to look for the next node within the quote. E.g.
C#
foreach (XmlElement xn in xnList)
{
    XmlNode xn2 = xn.SelectSingleNode("service/name");
    if (xn2 != null)
    {
        string NN = xn2.InnerText;
        if (NN != "")
        {
            names += NN;
            xn2 = xn.SelectSingleNode("rate/amount");
            string RR = xn2.InnerText;
            if(RR != "")
                names += "@" + RR;
            names += ",";
        }
    }
}
 
Share this answer
 
Comments
Manas Bhardwaj 7-May-14 11:32am    
+5. Lesser lines of code = (Relatively) lesser bugs!
CHill60 7-May-14 11:36am    
Thanks and LOL! I still prefer your version ... must still be a C++ programmer in me somewhere :-)
Maciej Los 7-May-14 16:21pm    
I like your new nick!
5ed!
CHill60 7-May-14 16:26pm    
Just for you :-)
Maciej Los 7-May-14 16:31pm    
;)

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