How to check if an XML element exists, and if does not exist for some transactions give default value?

Hi team,

I have an XML data that for some of the transactions an element is missing. My goal is whenever, that element is missing I’d like to write default value that says “Data does not exist” under that column/ element. I want to write the condition that allows me to proceed to the next transaction after populating the missing element with the default value.

Here is the screenshot of my workflow.

currentItem.Descendants(xns_WD+“Race_Ethnicity__Locale_Sensitive_”).First().Value

Thanks a lot for your help!

When working within Windows compatibility give a try at

currentItem.Descendants(xns_WD+"Race_Ethnicity__Locale_Sensitive_").Select(Function (x) x.Value).FirstOrDefault("NOTFOUNDDEFAULTTEXT")

Hi

Use an expression like this

If (String.IsNullOrEmpty(currentItem.Descendants(xns_WD+“Race_Ethnicity__Locale_Sensitive_”).First().Value), “ Data does not exist”, currentItem.Descendants(xns_WD+“Race_Ethnicity__Locale_Sensitive_”).First().Value)

@Sisay_Dinku

When working in Legacy / also working in Windows we can do:

currentItem.Descendants(xns_WD+"Race_Ethnicity__Locale_Sensitive_").Select(Function (x) x.Value).DefaultIfEmpty("NOTFOUNDDEFAULTTEXT").First()

In addition to:

When an element is not found a First() will return an exception and we will not use within an If

Here we would work e.g. with FirstOrDefault / isNothing()

1 Like

Hi @Palaniyappan @ppr

I tried to run it again using
If (String.IsNullOrEmpty(currentItem.Descendants(xns_WD+“Race_Ethnicity__Locale_Sensitive_”).First().Value), “ Data does not exist ”, currentItem.Descendants(xns_WD+“Race_Ethnicity__Locale_Sensitive_”).First().Value)
Query and it failed on the same transaction.
Just wanted to share how the missing element look like in XML


A you can see in the above, for the first portion the element does not exist and it can be found on the second one.
@ppr this query
currentItem.Descendants(xns_WD+“Race_Ethnicity__Locale_Sensitive_”).Select(Function (x) x.Value).FirstOrDefault(“NOTFOUNDDEFAULTTEXT”)
did not work for me.
Looking forward to hearing from you.
Thanks a lot!

what was done in detail?

FYI - I used the above expression inside the Append Item To List activity like you see below.
Not sure if that was the right thing to do.


Thank you

ok, just have a look on the above screenshots from the immediate panel

As we cleared the syntax and also verified, what is working with some sample data

When:

is used, what is returned?
Ensure:

OR use below

Show us also a screenshot with details from the loop part, where currentItem is coming from. Thanks

Hi @ppr

This worked fine.

I am able to extract the data as I needed. Thanks a lot!

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