C#中的linq to xml,带有属性的嵌套查询

4

我真的很难理解这个。

我正在使用c#。

我想从一个xml文件中获取一个产品的IEnumerable列表。

以下是xml结构的示例。

我需要获取一个产品列表,其中productEnriched自定义属性设置为true。

有些产品根本没有任何自定义属性部分

仅考虑这个问题我的头就开始疼了!

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

感谢任何帮助

为了澄清事情,我已经在示例XML中添加了更多项目

我需要获取产品列表 仅包含具有自定义属性元素且属性productEnriched的值为true的产品 XML中的某些产品将没有任何自定义属性或自定义属性元素 某些产品会有它,但其值为false 我只需要存在并具有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>

为什么要使用<custom-attribute attribute-id="productEnriched">true</custom-attribute>?至少<custom-attribute productEnriched="true">有什么问题吗? - Konrad Morawski
我需要提取一个具有嵌套自定义属性值为true的产品列表。对于为什么的问题,我不太理解,抱歉。我已经尝试了所有我能想到的方法,甚至让自己陷入疯狂的境地!我对C#和LINQ都还很新,这也没有帮助太多! - Pete
1个回答

3
我需要获取一个产品列表,只包含具有自定义属性元素的产品,该元素的属性为productEnriched,值为true。在xml中,有些产品没有任何自定义属性或自定义属性元素,有些产品可能会有,但其值为false。我只需要存在且值为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"));

顺便说一下,您的XML文件无效-起始标签(catalog)与结束标签(category)不匹配。这种格式本身就很奇怪,是你的想法吗?
    <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>

为什么要将属性名称作为属性值,将属性值作为元素值?这看起来很臃肿,有点像无明确目的地“重新发明”XML。

为什么不这样做:

    <custom-attributes>
       <custom-attribute productEnriched="true"/>
       <custom-attribute somethingElse="4"/>
       <custom-attribute anotherThing="otherdata"/>
    </custom-attributes>

或者:

    <custom-attributes productEnriched="true" somethingElse="4" anotherThing="otherdata"/>

或者只需使用元素:

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

谢谢,你真是个明星。看起来读一个解决方案要容易得多! - Pete
目录和类别那个是我手打例子时打错了。 - Pete
不,这不是我的文件,我无法控制它,只能接受它的现状。虽然这不是我想要的方式,但有些事情你只能接受!再次感谢。 - Pete
好的,没问题 :) 我根据使用了 mynamespace 这个词汇来判断,只是为了以防万一而进行了备注。感谢您接受我的答案 - 很高兴我能帮到您。 - Konrad Morawski

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接