Help parsing xml with two namespaces in same line

Hi

Normally parsing of the xml files is tedious but pretty straight forward.

But this one is giving me issues.

<s:Envelope xmlns:s=“http://schemas.xmlsoap.org/soap/envelope/”>
<s:Body>

<GetVehicleProductResult xmlns:a=http://schemas.data.org/2000/01/Webservice xmlns:i=http://www.w3.org/dpessfg/XMLSchema-instance>
<a:VFIInformation>
<a:BusinessUnitId>30</a:BusinessUnitId>

Normally I would assign the Namespaces to variables and do a deserialize on the string

and loop through the elements

For each

xmlResult.Element(NS1+“Envelope”).Element(NS1+“Body”).Element(NS2+“GetVehicleProductResponse”).Element(NS3+“GetVehicleProductResult”).etc

How should I write the for each string with the two namespaces in bold - NS1 and NS2 should be fine, but the “for each” gives an error becuase of the two namespaces.

@Michaeljep
Assuming you’ve already defined NS1 and NS2 for the “s” and “a” namespaces:

Assign: NS1 = XNamespace.Get(“http://schemas.xmlsoap.org/soap/envelope/”)
Assign: NS2 = XNamespace.Get(“http://schemas.data.org/2000/01/Webservice”)

Assign: xmlString = ‘Your XML string here’

Assign: xmlDoc = XDocument.Parse(xmlString)

For Each element As XElement In xmlDoc.Descendants(NS1 + “Envelope”).Elements(NS1 + “Body”).Elements(NS2 + “GetVehicleProductResult”).Elements(NS2 + “VFIInformation”)
’ Access elements within the loop, e.g., element.Element(NS2 + “BusinessUnitId”).Value
Next

1 Like

Hi,

What is .etc after Element(NS3+“GetVehicleProductResult”)?
I suppose it should be as the following.

xDoc.Element(NS1+"Envelope").Element(NS1+"Body").Element(NS2+"GetVehicleProductResponse").Element(NS3+"GetVehicleProductResult").Elements

image

Regards,

Etc. was just to illustrate that the xml continues.

I would like to iterate through the VFIINformation

1 = Namespace 1
2= Namespace 2
3 and 4 = how should these be written? as two namespaces? or combined?

xDoc.Element(NS1+“Envelope”).Element(NS1+“Body”).Element(NS2+“http://Webservice.org/”).Element - This is where im’m stuck…

Have a look here:

Especially on anlysing the elements and its corresponding namespaces by using the …Descendants()(index) option within the immediate panel

here we would, depending on the analysis result, also construct a XNamespace variable and use it.

BTW: there are also techniques where we can grab all namespace variables dynamic and store it within a lookup dictionary. This LookUp Dictionary we use later for the retrieval

Hi,

As 3 and 4 are the definitions of the namespace prefixes for “a” and “i”, respectively, we just use either of them if there is prefix a or i. (Probably we can ignore it when get GetVFListForVehicleProductResult element)

Can you share your xml and expected output as file, if possible? We might be able to write workflow.

Regards,

Hi

Here is the xml I’m trying to iterate

xmluipathforum.txt (8.9 KB)

The output should be a datatable of the VFIInformation that I can use.

Hi,

How about the following?

Sample20230928-1L.zip (10.0 KB)

Regards,

A quick Dirty oneliner

JArray.FromObject(xDoc.Descendants(xnsA + "VFIInformation").Select(Function (x) x.Elements().ToDictionary(Function (e) e.Name.LocalName, Function (e) e.Value))).ToObject(Of DataTable)

But we can decompose it also to

  • needed data column name retrieval for preparing the datatable schema construction
  • breaking it down into a nested loop for the value-grabbing