简体   繁体   English

使用linq xml的“ where”查询

[英]“where” query using linq xml

been taxing my brain trying to figure out how to perform a linq xml query. 一直在努力弄清楚如何执行linq xml查询。

i'd like the query to return a list of all the "product" items where the category/name = "First Category" in the following xml 我希望查询返回所有“产品”项目的列表,其中以下xml中的类别/名称=“第一类别”

<catalog>
  <category>
    <name>First Category</name>
    <order>0</order>
    <product>
      <name>First Product</name>
      <order>0</order>
    </product>
    <product>
      <name>3 Product</name>
      <order>2</order>
    </product>
    <product>
      <name>2 Product</name>
      <order>1</order>
    </product>
  </category>
</catalog>

Like so: 像这样:

    XDocument doc = XDocument.Parse(xml);
    var qry = from cat in doc.Root.Elements("category")
              where (string)cat.Element("name") == "First Category"
              from prod in cat.Elements("product")
              select prod;

or perhaps with an anonymous type too: 或者也可以使用匿名类型:

    XDocument doc = XDocument.Parse(xml);
    var qry = from cat in doc.Root.Elements("category")
              where (string)cat.Element("name") == "First Category"
              from prod in cat.Elements("product")
              select new
              {
                  Name = (string)prod.Element("name"),
                  Order = (int)prod.Element("order")
              };
    foreach (var prod in qry)
    {
        Console.WriteLine("{0}: {1}", prod.Order, prod.Name);
    }

Here's an example: 这是一个例子:

        string xml = @"your XML";

        XDocument doc = XDocument.Parse(xml);

        var products = from category in doc.Element("catalog").Elements("category")
                       where category.Element("name").Value == "First Category"
                       from product in category.Elements("product")
                       select new
                       {
                           Name = product.Element("name").Value,
                           Order = product.Element("order").Value
                       };
        foreach (var item in products)
        {
            Console.WriteLine("Name: {0} Order: {1}", item.Name, item.Order);
        }

You want to use the Single extension method here. 您要在此处使用Single扩展方法。 Try the following: 请尝试以下操作:

var category = doc.RootNode.Elements("category").Single(
    c => c.Attribute("name").Value == "First Category");
var products = category.Elements("product");

Note that this assumes you only have one category with name "First Category". 请注意,这假设您只有一个名为“第一类”的类别。 If you possibly have more, I recommend using Marc's solution; 如果可能的话,我建议使用Marc的解决方案。 otherwise, this should be the more appropiate/efficient solution. 否则,这应该是更合适/更有效的解决方案。 Also, this will throw an exception if any category node doesn't have a name child node. 此外,如果任何category节点都没有name子节点,则这将引发异常。 Otherwise, it should do exactly what you want. 否则,它应该完全按照您的要求进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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