Quantcast
Channel: LINQ to XML query from querystring - Stack Overflow
Viewing all articles
Browse latest Browse all 2

LINQ to XML query from querystring

$
0
0

I need to create xml files based on querystring values.

My xml file:

<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>

So if i type ?filter=Books,Paper i need to select Product where Categorycontains value from querystring.

Then if i type ?filter=Books,Paper&filter2=Drama i still need Product where Category contains filter1 but if Product element containsSubCategory that containsfilter2 i need to select just those.

So with: ?filter=Books,Paper&filter2=Drama i need to get xml that looks like this:

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

Also some products may have empty SubCategory element. I don't know if that is important.

My query looks like this:

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                        };

This is selecting all Product elements where filter1 contains SubCategory.

So can anyone point me in to right direction on how to write this query.

Thanks.


Viewing all articles
Browse latest Browse all 2

Trending Articles