3

我又来了……我有一个 XML 文件,其中包含我想查询不同属性的不同类别。

        <item>      
            <title>Industries</title>      
            <category type="Channel">Automotive</category>      
            <category type="Type">Cars</category>      
            <category type="Token">Article</category>      
            <category type="SpecialToken">News</category>      
            <guid>637f0dd7-57a0-4001-8272-f0fba60feba1</guid>
        </item>

在 SQL 中,我会写类似的东西。

select * from articles where channel = 'Automative' AND type = 'Cars' etc. etc.

我怎样才能用 linq 做到这一点?我尝试了以下查询,但它返回 null。如果我将这两个属性与“或”|| 操作员我会得到结果,但如果一个项目同时符合两个条件,则会得到所有双重结果。

var articleList = (from item in doc.Descendants("item")

                         from _category in item.Elements("category")
                         where _category.Value == valueCboChannel && _category.Attribute("domain").Value == "Channel"
                         && (_category.Value == valueCboSubChannel && _category.Attribute("domain").Value == "SubChannel")

                         select new
                         {

                             Title = item.Element("title").Value,
                             Guid= item.Element("guid").Value,
                             description = item.Element("description").Value,
                             link = item.Element("link").Value
                         }).ToList();

            ListView1.DataSource = articleList;
            ListView1.DataBind();               
4

2 回答 2

3

我会使用扩展方法来简化它来进行棘手的查找:

(更新 12/02/09 以排除空元素)

static string CategoryValue(this XElement element, string type)
{
    var category = element.Elements("category").FirstOrDefault(
        c => (string)c.Attribute("type") == type
            && !string.IsNullOrEmpty(c.Value)); // UPDATE HERE
    return category == null ? null : category.Value;
}

static void Main()
{
    XDocument doc = XDocument.Parse(xml);
    var qry = from item in doc.Descendants("item")
              where item.CategoryValue("Channel") == "Automotive"
              && item.CategoryValue("Type") == "Cars"
              select item;
    foreach (var node in qry)
    {
        Console.WriteLine(node.Element("guid").Value);
    }
}
于 2009-02-11T09:17:26.810 回答
0

感谢您的提示,我设法以某种方式使其工作,但仅在扩展方法中使用“.First”。

我现在面临的问题是如何获取 xml 文件中有 NULL 值的所有值。基本上,xml 可能如下所示。因此,如果我使用“First”,那么当然会选择第一个空的,因此不会显示。有没有办法跳过 NULL 值?

<item>
    <title>Industries</title>
    <category type="Channel"></category> 
    <category type="Channel">Automotive</category> 
    <category type="Type"></category>     
    <category type="Type">Cars</category>     
    <category type="Token">Article</category>       
    <category type="SpecialToken">News</category>       
    <guid>637f0dd7-57a0-4001-8272-f0fba60feba1</guid>  
</item>

这里是当前的扩展方法

    public static string CategoryValue(this XElement item, string type)
    {
        var category = item.Descendants("category").First(c => (string)c.Attribute("type") == type);
        return category == null ? null : category.Value;

    }
于 2009-02-12T15:26:19.283 回答