Eric
Eric

Reputation: 135

XML use SelectNodes to get nodes that have a subnode with a null value

Currently we get a list of nodes where a specific node has either 0.00, 0 or 0,00 value.

hoofddocument.SelectNodes("//Product[./polispremie_1[.='0,00' or .='0' or .='0.00' ]]")

But we also want to include the Product nodes if the 'polispremie_1' node is null.

I would prefer to add something to the current xpath.

I have tried adding ' or .= null ' but that doesn't work.

Example of the xml:

<Products>
    <Product>
        <polispremie_1/>
    </Product>
    <Product>
        <polispremie_1>0</polispremie_1>
    </Product>
</products>

In this example the first product has polispremie_1 with null value. I would like to get both with the above mentioned query, but I only get the second product.

Upvotes: 0

Views: 130

Answers (2)

dbasnett
dbasnett

Reputation: 11773

A VB example

    Dim testXE As XElement = <Products>
                                 <Product>
                                     <polispremie_1/>
                                 </Product>
                                 <Product>
                                     <polispremie_1>0</polispremie_1>
                                 </Product>
                                 <Product>
                                     <polispremie_1>0.0</polispremie_1>
                                 </Product>
                             </Products>

    Dim ie As IEnumerable(Of XElement)
    Dim d As Decimal
    ie = From el As XElement In testXE...<polispremie_1>
           Where (Decimal.TryParse(el.Value, Globalization.NumberStyles.Any,
                                  Globalization.CultureInfo.InvariantCulture,
                                  d) AndAlso d = 0) OrElse el.Value = ""
           Select el

    'look at results in ie

Upvotes: 1

Martin Honnen
Martin Honnen

Reputation: 167426

It is always hard to tell what people consider a null in XML but //Product[not(polispremie_1) or polispremie_1[.='0,00' or .='0' or .='0.00' ]] would select Product elements not having a polispremie_1 child and the ones for which your condition of value comparison holds.

Upvotes: 0

Related Questions