XML with Namespace - extract sibling value or default value when element is empty or missing

How to validate if a element exist or is empty.

Sometimes, the element @@FechaPago is empty or simply, the element is not found in the xml, so I want to check if the element exists with name==@@FechaPago and value is not empty then it extracts the value and fecha_pago==“05-12-2022” else my variable fecha_pago==“N/A”

 <n3:ExtensionContent>
        <n4:AdditionalInformation>
          <n4:AdditionalProperty>
            <n2:ID />
            <n2:Name>@@FamiliaProducto</n2:Name>
            <n2:Value>Recaudaciones y Pagos</n2:Value>
          </n4:AdditionalProperty>
          <n4:AdditionalProperty>
            <n2:ID />
            <n2:Name>@@TipoPrestamo</n2:Name>
            <n2:Value>Servicios Especiales por Consulta. Credipago</n2:Value>
          </n4:AdditionalProperty>
          <n4:AdditionalProperty>
            <n2:ID />
            <n2:Name>@@NumeroContrato</n2:Name>
            <n2:Value>191 ****079 * **</n2:Value>
          </n4:AdditionalProperty>
          <n4:AdditionalProperty>
            <n2:ID />
            <n2:Name>@@FechaPago</n2:Name>
            <n2:Value>2022-11-28</n2:Value>
          </n4:AdditionalProperty>
          <n4:AdditionalProperty>
            <n2:ID />
            <n2:Name>@@CodFamiliaProducto</n2:Name>
            <n2:Value>RP</n2:Value>
          </n4:AdditionalProperty>
          <n4:AdditionalProperty>
            <n2:ID />
            <n2:Name>@@CodAplicativo</n2:Name>
            <n2:Value>CREP</n2:Value>
          </n4:AdditionalProperty>
          <n4:AdditionalProperty>
            <n2:ID />
            <n2:Value />
          </n4:AdditionalProperty>
        </n4:AdditionalInformation>
      </n3:ExtensionContent>

Similar to

give a try:

(From xe In xmlDes.Root.Descendants(dicNamespaces("n4") + "AdditionalProperty")
Where xe.Element(dicNamespaces("n2") + "Name").Value.Trim.Equals("@@FechaPago")
Select v=  xe.Element(dicNamespaces("n2") + "Value").Value).DefaultIfEmpty("N/A").First()

@ppr good morning, i want only validate if the elemento @@FechaPago exist, and the activity return 0 if don’t exist, 1 if exist.

hasFound | Boolean =

(From xe In xmlDes.Root.Descendants(dicNamespaces("n4") + "AdditionalProperty")
Where xe.Element(dicNamespaces("n2") + "Name").Value.Trim.Equals("@@FechaPago")
Select e=  xe).Any

will return true if one or more @@FechaPago exists

I have a error

ensure within the debugging / panels that xmlDes is of Datatype XDocument and not null
similar to the dicNamespaces are to check that it is correct

Maybe you can also share the XML file with us

Your statement work when the elemento name==@@fechaPago exist, but when name==@@fechaPago doesn’t exist in xml I get the message of error.

BCP_des.xml (22.3 KB)

In this example the element with value"@@fecha de Pago" doesn’t exist.

Based on this example we would recommend first to check if the ni namespace is within the dictionary:

dicNameSpaces.ContainsKey("n2")

When it is within the name space then we can go ahead.

Maybe you can also share a complete sample XML with existing n2 Elements, so we can evaluate the statement.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.