C# linq to xml, nested query with attributes
c#, linq, xml
Solution
I need to get a list of products only products that have a custom-attribute element with the attribute productEnriched and value of true some products in the xml wont have any custom-attribute or custom-attributes elements some products will have it but with a value of false i just need a list of products where it exists and has a value of true
var xml = XElement.Load(@"your file.xml");
XNamespace ns = "http://www.mynamespace.com";
var products = xml.Elements(ns + "product");
var filtered = products.Where(
product =>
product.Element(ns + "custom-attributes") != null &&
product.Element(ns + "custom-attributes").Elements(ns + "custom-attribute")
.Any(
ca =>
ca.Value == "true" &&
ca.Attribute("attribute-id") != null &&
ca.Attribute("attribute-id").Value == "productEnriched"));
By the way, your XMLs are not valid - your opening tag (`catalog`) does not match your closing tag (`category`).
The format by itself is strange - is it your idea?
<custom-attributes>
<custom-attribute attribute-id="productEnriched">true</custom-attribute>
<custom-attribute attribute-id="somethingElse">4</custom-attribute>
<custom-attribute attribute-id="anotherThing">otherdata</custom-attribute>
</custom-attributes>
Why put an attribute name as an attribute value and attribute value as an element value? It looks bloated and kind of "reinvents" XML with no clear purpose.
Why not:
<custom-attributes>
<custom-attribute productEnriched="true"/>
<custom-attribute somethingElse="4"/>
<custom-attribute anotherThing="otherdata"/>
</custom-attributes>
Or:
<custom-attributes productEnriched="true" somethingElse="4" anotherThing="otherdata"/>
Or perhaps just use elements:
<product-parameters>
<productEnriched>true</productEnriched>
<somethingElse>4</somethingElse>
<anotherThing>otherdata</anotherThing>
</product-parameters>
Problem
Im really struggling to get my head round this. Im using c#. I want to get back an IEnumerable of products from an xml file. Below is a sample of the xml structure. I need to get a list of products that have the productEnriched custom attribute set as true. Some products wont have any custom attribute section at all my head has strated to hurt just thinking about it! ``` <?xml version="1.0" encoding="UTF-8"?> <catalog xmlns="http://www.mynamespace.com" catalog-id="MvgCatalog"> <product> <custom-attributes> <custom-attribute attribute-id="productEnriched">true</custom-attribute> </custom-attributes> </product> </category> ``` thanks for any help To clear things up i have added a few more items to the example xml I need to get a list of products only products that have a custom-attribute element with the attribute productEnriched and value of true some products in the xml wont have any custom-attribute or custom-attributes elements some products will have it but with a value of false i just need a list of products where it exists and has a value of true ``` <?xml version="1.0" encoding="UTF-8"?> <catalog xmlns="http://www.mynamespace.com" catalog-id="MvgCatalog"> <product> <upc>000000000000</upc> <productTitle>My product name</productTitle> <custom-attributes> <custom-attribute attribute-id="productEnriched">true</custom-attribute> <custom-attribute attribute-id="somethingElse">4</custom-attribute> <custom-attribute attribute-id="anotherThing">otherdata</custom-attribute> </custom-attributes> </product> </category> ```