【问题标题】:query xmlnode using linq使用 linq 查询 xmlnode
【发布时间】:2009-02-23 06:20:32
【问题描述】:

我有以下文件:

<root>
  <Product desc="Household">
    <Product1 desc="Cheap">
        <Producta desc="Cheap Item 1" category="Cooking" />
        <Productb desc="Cheap Item 2" category="Gardening" />
    </Product1>
    <Product2 desc="Costly">
        <Producta desc="Costly Item 1" category="Decoration"/>
        <Productb desc="Costly Item 2" category="Furnishing" />
        <Productc desc="Costly Item 3" category="Pool" />
    </Product2>
  </Product>
</root>

我想了解以下信息:Cheap and Costly 中的商品总数, 所有类别的列表(如烹饪、园艺、装饰......),排序类别列表并仅选择“昂贵”的产品

如何使用 LINQ。 我一直这样做到现在:

 XElement xe = XElement.Load(Server.MapPath("~/product.xml"));
 ????

【问题讨论】:

    标签: c# linq linq-to-xml


    【解决方案1】:

    不幸的是,您的 XML 结构将 Product 元素用于层次结构的三个级别。你还有其他类似“家庭”的元素吗?

    假设我们只想要家用的,您可以使用:

    计算每个便宜/昂贵的项目

    xe.Element("Product") // Select the Product desc="household" element
      .Elements() // Select the elements below it
      .Select(element => new { Name=(string) element.Attribute("desc"),
                               Count=element.Elements().Count() });
    

    列出所有类别

    xe.Descendants() // Select all descendant elements
      .Attributes() // All attributes from all elements
      // Limit it to "category" elements
      .Where(attr => attr.Name == "category")
      // Select the value
      .Select(attr => attr.Value)
      // Remove duplicates
      .Distinct();
    

    要对此进行排序,只需在末尾使用.OrderBy(x =&gt; x)

    选择“昂贵”的产品

    xe.Descendants() // Select all elements
      // Only consider those with a "Costly" description
      .Where(element => (string) element.Attribute("desc") == "Costly")
      // Select the subelements of that element, and flatten the result
      .SelectMany(element => element.Elements());
    

    【讨论】:

    • 我真的需要自学 LINQ...这样听起来太容易了 :)
    • 我怀疑 Jon 是否打算让您重命名数据的单个元素;而不是在每个 level... 有不同的名称
    • 好吧..对不起..你知道早期的 xml 中一切都是产品,当我使用你的第一个查询时,它给了我对象未设置为引用错误
    • 嗯...它应该可以正常工作,因为我使用您提供的 XML 进行了测试(加上两个“/”来终止元素)。
    • 感谢您的及时回复。请详细说明(加上两个“/”来终止元素)?
    【解决方案2】:

    嗯,我个人觉得XmlDocument 更容易:

        XmlDocument root = new XmlDocument();
        root.LoadXml(xml); // or .Load(path);
    
        var categories = root.SelectNodes(
            "/root/Product/Product/Product/@category")
            .Cast<XmlNode>().Select(cat => cat.InnerText).Distinct();
        var sortedCategories = categories.OrderBy(cat => cat);
        foreach (var category in sortedCategories)
        {
            Console.WriteLine(category);
        }
    
        var totalItems = root.SelectNodes(
             "/root/Products/Product/Product").Count;
        Console.WriteLine(totalItems);
    
        foreach (XmlElement prod in root.SelectNodes(
            "/root/Product/Product[@desc='Costly']/Product"))
        {
            Console.WriteLine(prod.GetAttribute("desc"));
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-09
      • 2010-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多