Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.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# 如果条件为true,则从xml向textbox添加指定元素_C#_Xml_Linq To Xml - Fatal编程技术网

C# 如果条件为true,则从xml向textbox添加指定元素

C# 如果条件为true,则从xml向textbox添加指定元素,c#,xml,linq-to-xml,C#,Xml,Linq To Xml,我正在尝试将XML文件中的字符串添加到文本框中,但尚未成功。我要做的是检查“date”元素,如果“bejovo”与“date”元素匹配,则将元素“name”值放入列表中,并计算价格。这是我的代码 if (File.Exists(path)) { XDocument doc = XDocument.Load(path); var c = from x in doc.Descendants("order")

我正在尝试将XML文件中的字符串添加到文本框中,但尚未成功。我要做的是检查“date”元素,如果“bejovo”与“date”元素匹配,则将元素“name”值放入列表中,并计算价格。这是我的代码

if (File.Exists(path))
            {
                XDocument doc = XDocument.Load(path);
                var c = from x in doc.Descendants("order")
                        where x.Element("date").Value == bejovo
                        select new
                        {
                            //??
                        };
                foreach (var item in c)
                {
                    textBox1.Text = item.ToString();                    
                }
            }
这是我的XML文件:

<user id="0">
    <order id="0">
      <date>2012.11.20. 1:29:20</date>
      <menuelem db="0">
        <name>Pizza</name>
        <price>1290</price>
      </menuelem>
      <menuelem db="1">
        <name>Coke</name>
        <price>300</price>
      </menuelem>
    </order>
  </user>
<user id="0">
    <order id="1">
      <date>2012.11.19. 21:49:29</date>
      <menuelem db="0">
        <name>Milk</name>
        <price>200</price>
      </menuelem>
    </order>
  </user>

2012.11.20. 1:29:20
披萨
1290
焦炭
300
2012.11.19. 21:49:29
鲜奶
200

因此,如果是bejovo=“2012.11.20.1:29:20”,那么我的结果必须是“Pizza”和“Coke”,价格是1590。

这个代码肯定没有验证:

XDocument doc = XDocument.Load("In.xml");
var c = from x in doc.Descendants("order")
        where x.Element("date").Value == "2012.11.20. 1:29:20"
        select new
        {
            Names = string.Join(", ", x.Elements("menuelem")
                                       .Elements("name")
                                       .Select(s => s.Value)),
            Price = x.Elements("menuelem")
                     .Elements("price")
                     .Select(s => decimal.Parse(s.Value))
                     .Sum()
        };
foreach (var item in c)
{
    textBox1.Text = string.Format("{0}\tprice:{1}", item.Names, item.Price);
}

输出结果如下:
比萨饼,可乐价格:1590

可能会尝试类似的方法。下面的代码未经测试,目前无法在Visual Studio中加载和测试

var items = doc
   .Elements("user")
   .Elements("order")
   .Where(o => (string)o.Element("date") == bejovo)
   .Elements("menuelem")
   .Select(m => new 
   { 
     Name = (string)m.Element("name"), 
     Price = (int?).Element("price") 
   };
var names = items.Select(i => i.Name).ToList();
var price = items.Select(i => i.Price).Sum();