Get processing instructions from XML file

Hi Experts

I’m fairly new to the processing of XML files but have managed to extract most of the elements from my file.

But how do I get the processing instructions from the top of the XML file?

<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<?vaxTimestamp 2021-02-01T20:05:13?>
<?vaxDocId 1ys6s72-8_7YJ6HGG6?>

I need to extract the vaxTimestamp and vaxDocId. If I loop through all the element of the XML file I can see they are of the type Processing Instructions.

Any ideas?

Hi @jacchr
You can try with regex
varTimestamp ReGEX PATTERN : (?<=vaxTimestamp ).*(?=[?>])

image

vaxDocId Regex pattern : (?<=vaxDocId ).*(?=[?>])

Regards
Nived N
Happy Automation

Hi Nived_Nambiar

Thanks - but I’d rather avoid using RegEx now that it is an element that somehow can be referenced. I can use xmlDoc.FirstNode.ToString as well - but I’d like to be able to use the name/ID of the processing instruction instead like I do with all the other nodes/elements.

@jacchr
having a sample string, parsing it to a xDocument with the help of Deserialize XML Activity (Package UiPath.WebAPI.Activities) has following result:

filtering all PIs

Access the PI Value:

grafik
Some samples on:
getting entire PI - SamplePI.toString
getting PI Value - SamplePI.Data
getting PI Name - SamplePI.Target

Also have a look on this activity:
grafik

Thanks @ppr

I ended up using xmlDoc.Nodes.OfType(Of XProcessingInstruction).Cast(Of XProcessingInstruction).ToList which is very similar to your solution.

@ppr

Would you by any chance know if it is possible to get .Data from from .Target matching a specific name?

E.g.

xDoc.Nodes().Where(Function(x) x.NodeType = XmlNodeType.ProcessingInstruction).Cast(Of XProcessingInstruction).First().Where(Function(y) y.Target = “vaxTimestamp”).Data

@jacchr
the base type for LINQ is the IEnumerable. Have a look here:

@Dave gave us a good explanation on what it is here:

The nature of Where is returning us an IEnumerable(Of T) (just simplified) and can contain:

  • 0
  • 1
  • multiple
    Items

So we have to ensure in your case, that we catch the data from 1 and only one element (think on no filter return and/or multiple catches).

For this we can work with DefaultIfEmpty and mixing Method and Query Syntax:

(From pi In xDoc.Nodes().Where(Function (x)  x.NodeType = XmlNodeType.ProcessingInstruction).Cast(Of XProcessingInstruction)
Where pi.Target.Equals(PIFilterKey)
Select pi).DefaultIfEmpty(New XProcessingInstruction("NoName","UNDEFINED")).First().Data

line1: fetch all PIs
line2: Filter all PIs with the name equal to the FilterKey
line3: for an empty result return a dummy PI, other wise return the found PIs, from this take the first PI and its Data

In case of we would expect multiple PI as a filer result we would return it e.g. as List/Array and process / evaluate later.

grafik
Output when found:
grafik

Output empty filter result:
grafik

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