Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查DropDownList中选择的值,并对照XML值进行检查_C#_Asp.net_Xml - Fatal编程技术网

C# 检查DropDownList中选择的值,并对照XML值进行检查

C# 检查DropDownList中选择的值,并对照XML值进行检查,c#,asp.net,xml,C#,Asp.net,Xml,基本上,我要做的是检查dropDownList1中选择的值是否在XML文档中,如果是,则打印出它的fat内容。否则我将返回字符串“抱歉,我们找不到您的食物!”。就目前而言,我只得到了另一种情况。任何帮助都会很棒 我的代码如下: XmlDocument xDoc = new XmlDocument(); xDoc.Load("the xml address"); // go through each food name (this works)

基本上,我要做的是检查dropDownList1中选择的值是否在XML文档中,如果是,则打印出它的fat内容。否则我将返回字符串“抱歉,我们找不到您的食物!”。就目前而言,我只得到了另一种情况。任何帮助都会很棒

我的代码如下:

 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!";
               }
           }

        }

希望我解释得足够好,谢谢各位。

知道dropdownlist的选定值的属性是
SelectedValue
类似:

DropDownList1.SelectedValue  == foodName
我不认为需要第二个循环,因为它甚至不验证是否选中了该项

或者,您可以使用LINQ在XML中获取食物项目列表,具体操作如下:

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"),
};  
然后,您可以从不同的数组函数中获益,如:

Array.Exists(items, DropDownList1.SelectedValue)
或者在LINQ查询中使用
where
let
关键字尽可能地使用它进行筛选:

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

将foreach循环替换为以下内容:

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

}

您可以使用XPath表达式获取子节点等于所选食物的
节点,然后获取子节点。多功能XPath表达式,因此无需手动循环每个
节点即可完成此操作。例如:

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!";
}