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が返されます。2つの属性を「OR」と組み合わせると|| 演算子私は結果を取得しますが、アイテムが両方の基準に一致する場合はすべて二重の結果になります。

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 に答える