简体   繁体   中英

Check what value selected in a DropDownList and check it against XML value

what I'm essentially trying to do is check to see if the value selected in my dropDownList1 is in my XML document, and if so print out it's fat content. Otherwise I return the string "Sorry we can't find your food!". As it stands I'm only getting the else scenario . Any help would be fantastic

my code is as follows:

 XmlDocument xDoc = new XmlDocument();
        xDoc.Load("the xml address");

        // go through each food name (this works)
        foreach (XmlNode name in xDoc.SelectNodes("Web_Service/Food/Name")) 

        {
           //grab the string
           string foodName = name.InnerText;

           // what I'm tring to here is loop through the items in the dropdown list
           // and check it against foodName
           foreach (ListItem li in DropDownList1.Items)
           {
               if (li.Value == foodName)
               {
                   // print the value fat value of that particular foodName
                  // address is ("Web_Service/Food/Fat")
               }
               else
               {
                   TextBox2.Text = "sorry we could not find your food!";
               }
           }

        }

Hopefully I explained it well enough, thanks guys.

Replace your foreach loop with the following:

string nodeSelect = String.Format("Web_Service/Food/Name[@Value='{0}']",foodName);

XmlNodeList nodes = xDoc.SelectNodes(nodeSelect);

if (nodes.Count == 0)
{

TextBox2.Text = "sorry we could not find your food!";

}

else

{

//print the value fat value of that particular foodName
// address is ("Web_Service/Food/Fat

}

The property to know the selected value of a dropdownlist is SelectedValue like:

DropDownList1.SelectedValue  == foodName

I do not see the need of your second loops as it does not even verify if the item is selected.

Alternatively you could use LINQ to get the list of food items in your XML doing something like:

XDocument doc=XDocument.Load(Server.MapPath("the xml address"));    

var items = (from item in doc.Root.Elements("Web_Service/Food") 
select new {     
    name=c.Element("Name"),
};  

Then you can benefit from different array functions like:

Array.Exists(items, DropDownList1.SelectedValue)

Or using it as possible filter in your LINQ Query using where and let keywords:

var items = (from item in doc.Root.Elements("Web_Service/Food") 
let name = c.Element("Name")
where DropDownList1.SelectedValue == name
select new {     
    name=name,
};  

You can use XPath expression to get <Food> node having child node <Name> equals selected food, then get the <Fat> child node. All in one XPath expression so you can accomplish this without manually looping through each <Food> nodes. For example :

string foodName = DropDownList1.SelectedValue;
string xpath = String.Format("Web_Service/Food[Name='{0}']/Fat", foodName);
XmlNode fatNode = xDoc.SelectSingleNode(xpath);
if(fatNode != null)
{
    TextBox2.Text = fatNode.InnerText;
}
else
{
    TextBox2.Text = "sorry we could not find your food!";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM