C # linq to xml, subquery with attributes

I'm really struggling to figure it out.

Im using c #.

I want to return IEnumerable products from an xml file.

The following is a sample xml structure.

I need to get a list of products for which the user attribute productEnriched is set to true.

Some products usually do not have a custom attribute section.

my head wanted 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 added some more elements to the xml example

I need to get a list of products only those products that have a custom-attribute with the productEnriched attribute and true some products in xml will not have any custom-attributes or custom-attributes some products will have it, but with a false value I just need a list of products where it exists and is 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> 
+4
source share
1 answer

I need to get a list of products only products that have a custom-attribute element with the productEnriched attribute and value from the true of some products in xml there will not be any user attribute or user attribute elements some products will have it, but with the value false, I just need a list of products where it exists and 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 XML is invalid - your catalog tag does not match your category tag.

The format itself is strange - is that 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 an attribute value as an element value? It looks bloated and kind of โ€œinventsโ€ XML without any 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 maybe just use the elements:

  <product-parameters> <productEnriched>true</productEnriched> <somethingElse>4</somethingElse> <anotherThing>otherdata</anotherThing> </product-parameters> 
+3
source

All Articles