0
我需要基於查詢字符串值創建xml文件。從查詢字符串LINQ到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
,我需要選擇Product
其中Category
包含查詢字符串值。
然後,如果我輸入?filter=Books,Paper&filter2=Drama
我仍然需要Product
其中Category
包含filter1
但如果Product
元素包含SubCategory
包含filter2
我需要選擇只是那些。
所以有:?filter=Books,Paper&filter2=Drama
我需要得到XML,看起來像這樣:
<Products>
<Product>
<Name>Name</Name>
<Category>Books</Category>
<SubCategory>Drama</SubCategory>
</Product>
<Product>
<Name>Name</Name>
<Category>Paper</Category>
<SubCategory></SubCategory>
</Product>
</Products>
也有一些產品可能有空SubCategory
元素。我不知道這是否重要。
我的查詢看起來是這樣的:
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
};
這是選擇所有Product
元素,其中包含filter1
SubCategory
。
所以任何人都可以指出我如何寫這個查詢的正確方向。
謝謝。