How to get Node with lowest value from XML using C# .Net

Last Reply on Jun 17, 2014 01:04 PM By Azim

Posted on Jun 17, 2014 05:41 AM

HI

I have response as follows

<hdetails>
<hcode>145852</hcode>
<avirm>
<hprice>1523.36</hprice>
<hname>Testing</hname>
</avirm>
<avirm>
<hprice>1004.47</hprice>
<hname>Tusssing</hname>
</avirm>
<avirm>
<hprice>1474.25</hprice>
<hname>ACDFRFVGV</hname>
</avirm>
</hdetails>
<hdetails>
<hcode>16572</hcode>
<avirm>
<hprice>852.36</hprice>
<hname>Tqawsting</hname>
</avirm>
<avirm>
<hprice>1664.47</hprice>
<hname>adarfsssing</hname>
</avirm>
<avirm>
<hprice>1007.25</hprice>
<hname>ACDAdfaFVGV</hname>
</avirm>
</hdetails>
<hdetails>
<hcode>16572</hcode>
<avirm>
<hprice>662.36</hprice>
<hname>avsvkbaskbv</hname>
</avirm>
<avirm>
<hprice>7986</hprice>
<hname>babasbaslbn</hname>
</avirm>
<avirm>
<hprice>2222.25</hprice>
<hname>sbaufasjgsb</hname>
</avirm>
</hdetails>

and i read it into datatable.

but from above ,if i want lowest hprice of hcode = 16572 , how to get that..because i have two response nodes with hcode = 16572

i want  lowest and save into datatable row..

Posted on Jun 17, 2014 01:04 PM

Namespace

using System.Xml;

 

protected void Page_Load(object sender, EventArgs e)
{
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(Server.MapPath("~/XML/XMLFile.xml"));
    XmlNodeList xnlNodes = xmlDoc.SelectNodes("/NewDataSet/hdetails/avirm");
    double minvalue = 0;
    List<double> price = new List<double>();
    foreach (XmlNode xndNode in xnlNodes)
    {
        if (((xndNode).PreviousSibling).InnerXml == "16572")
        {
            XmlNode parent = xndNode.ParentNode;
            XmlNodeList child = parent.ChildNodes;
            foreach (XmlNode childNode in child)
            {
                foreach (XmlNode childNode1 in childNode.ChildNodes)
                {
                    if (childNode1.Name == "hprice")
                    {
                        price.Add(Convert.ToDouble(childNode["hprice"].InnerText));
                    }
                }

            }
        }
    }
    minvalue = price.Min();
}
}