2011-06-27 11 views
0

クエリーストリング値に基づいてxmlファイルを作成する必要があります。クエリーストリングのLINQ to XMLクエリー

私のxmlファイル:

<Products> 
<Product> 
    <Name>Name</Name> 
    <Category>Books</Category> 
    <SubCategory>Drama</SubCategory> 
</Product> 
<Product> 
    <Name>Name</Name> 
    <Category>Books</Category> 
    <SubCategory>Action</SubCategory> 
</Product> 
<Product> 
    <Name>Name</Name> 
    <Category>Paper</Category> 
    <SubCategory></SubCategory> 
</Product> 

私は?filter=Books,Paperを入力した場合ので、私はCategoryは、クエリ文字列から値を含んでいる場合Productを選択する必要があります。私は?filter=Books,Paper&filter2=Dramaを入力した場合

は、私はまだProduct要素iがちょうどそれらを選択する必要がありfilter2が含まれているSubCategoryが含まれている場合Categoryfilter1が含まれていますが、どこProductを必要としています。

そうで:

<Products> 
    <Product> 
     <Name>Name</Name> 
     <Category>Books</Category> 
     <SubCategory>Drama</SubCategory> 
    </Product> 
    <Product> 
     <Name>Name</Name> 
     <Category>Paper</Category> 
     <SubCategory></SubCategory> 
    </Product> 
</Products> 

また、いくつかの製品が空SubCategory要素を有していてもよい:?filter=Books,Paper&filter2=Drama私はこのようなXMLを取得する必要があります。それが重要かどうかは分かりません。

私のクエリは次のようになります。これはfilter1SubCategoryが含まれているすべてのProductの要素を選択している

var items = from el in SimpleStreamAxis(esysPath, "Product") 
         where filter.Contains(el.Element("Category").Value.Trim()) 
         where filter1.Contains(el.Element("SubCategory").Value.Trim()) 
         select new 
         { 
          ProductID = el.Element("ID").Value, 
          Name = el.Element("Name").Value, 
          Price = el.Element("Price").Value, 
          Picture = el.Element("Picture").Value 
         }; 

誰でもこのクエリを書く方法について私に正しい方向を指すことができます。

ありがとうございました。

答えて

0

これは、あなたが正しい方向に始める必要があり、それはCategory要素がBookまたはPaper

List<string> categories = new List<string() {"Book", "Paper"}; 
XDocument doc = XDocument.Parse("Your xml string"); 
var products = doc.Descendants("Product") 
       .Where(el => categories.Contains(el.Element("Category").Value)); 
のいずれかであるすべての製品を検索します