Analyse xaml files - Tags and namespaces

Dear all,

I am developing a robot to analyse xaml files. This robot in attachment is able to find the node “ui:Target” and analyse the Selector tag.

However I would like to make it more general. Why only target? I would like my robot to analyse all of the selectors whose nodes start with “ui:”, but at the moment it does not work.

XamlAnalysis.zip (4.6 KB)

any suggestions?

Thanks
Kind regards
Gennaro

Hi.

So, I’m not really that familiar with XMLs but I looked it up and there’s a contains and starts-with function you can use.

diagramTree.DocumentElement.SelectNodes("//*[contains(name(),'ui:')]", selectorsNamespaceManager)

or

diagramTree.DocumentElement.SelectNodes("//*[starts-with(name(),'ui:')]", selectorsNamespaceManager)

After doing this though, some of the elements found won’t have a selector so you would need to either surround the Invoke Method with a Try/Catch or adjust the Input parameter so it doesn’t throw an error.

I hope this helps or atleast gets you on the right track.

Regards.

2 Likes

Hi @ClaytonM

thanks a lot for you reply! It works! I also have the following two challenges:

Challenge 1:
The code xmlDoc.Element(activities+"Activity").Element(activities+"Sequence").Attribute("DisplayName").Value finds the display name attribute of the Sequence element. However this Sequence element must be after the Activities element. But I would actually like to find all of the Sequence Elements in the code, including their (parent / child) path, independently on where these sequence Elements are.

Challenge 2:
Once I find an element such as ui element or sequence element, I would like to find all of its children, children of children etc, without losing the information about which level they are: level 1 (child) level 2 (child of child) etc.

Please let me know if you can help.

Thanks
Kind regards
Gennaro

Hi @ClaytonM

if you look at the file in attachment, I am trying to extract info about the children nodes belonging to a certain sequence.

Unfortunately it returns a strange error (it does not let me deserialize). Any chance you could have a look? My goal is reading all of the children nodes belonging to the extracted sequence.

XamlAnalysis.zip (8.1 KB)

Thanks
Kind regards
Gennaro

Hey @Gennaro_Bozza

I was getting an error that pointed to the position: “There are multiple root elements. Line 1, position 706” for example

So, I took your xml string, sequenceNode.Innerxml, and pasted the text into Word and performed a Word Count to get the character location that the error is pointing to. Then, just as a test, I split off all the text after that position and it got passed the error message.

Apparently, it’s not liking the fact that the beginning root elements are closed and a second group of root elements are started, if that makes sense. For example, where it says <\ui:OpenBrowser> closes the first root element, so everything after that point is causing the error.

I hope that helps identify the problem. This is not my expertise and I’m short of time, so I got to stop there in finding a solution for this.

Regards.

C

1 Like

Hi @ClaytonM

Many thanks for your reply, that’s great! However I’m indeed developing a pretty tough code.

I went ahead and now I would like to extract all of the children nodes from a certain sequence: for each child, analyse the child of the child, for each child of the child, if exist analyse the child of the child of the child and so on…

I did it with approach in attachment, but how can I do it avoiding nested for each?

Furthermore, the file in attachment has an error that occurred when I isolated this piece of code in order to send it to you… Any idea why?

NodesAnalysis.zip (11.0 KB)

Many thanks
Gennaro

Hi @ClaytonM any chance you could have a look? I’m trying with recursive functions but it does not work well yet.

Many thanks
Kind regards
Gennaro

There is an error because you are using a variable in the Default value of a variable both which are in the same scope so it has not yet been declared and can not be used to declare another variable.
" Application nodes : Object reference not set to an instance of an object."
Application nodes is the scope or topmost sequence. Basically you can just use the same line that you declared the variable as inplace of the variable name.
Like so,

New XmlNamespaceManager(new XmlDocument().NameTable)

But that might not be what you need. The alternative to that is to change the scope of the NamespaceManager variables to an inner scope so diagramTree is in an outer scope so it can be set before the other variables that use it, if that makes sense.

There must be a way to get all child nodes without looping through each one individually. I’ll need to play around with it and get back to you on that though.

There is also a way with lambda expressions in vb.net.
I have not looked into enough to confirm that, but basically you can take the list of nodes and run it through a .Select() and/or .Where()

Regards.

Hi @ClaytonM,

Thanks, that’s awesome! I Kind of managed it with a recursive Invoke. Very difficult stuff!

NodesAnalysisRecursive.zip (15.4 KB)

But what my code does not do yet is finding all of the ui:nodes.

Here’s the logic: if node is ui: then analyse node and then analyse Children if any. If node is not ui, then keep going and analyse it’s children if any.

In the code in attachment, you will see that it stops at the second Level (child of the child). why? How can I have my code perform what I would like?

Many thanks
Gennaro

You have a Try/Catch surrounding your stuff so what you can do to identify why it stopped is output what errors occurs, because it will skip anything where there is an error.
Using exception.Source+","+exception.Message I displayed the error here:
image
The “Anonymously hosted…” error tells me that one of your invokes has a variable with nothing in it or an out variable is empty.

So, I took a look at your variable/argument structure and it has me thinking that you need to adjust some things there. Like for example, a Main should not have any Out or In arguments, and in your Recursive workflow, you have Out arguments that basically duplicate the In arguments. If you have no intention of changing the value where the Main needs the new value then they should not be Outs or In/Outs.

I don’t know if this will help identify why it’s stopping, but hopefully it will.
Catching the error will help with troubleshooting though.

Regards.

Blockquote
Hi.
So, I’m not really that familiar with XMLs but I looked it up and there’s a contains and starts-with function you can use.
diagramTree.DocumentElement.SelectNodes(“//*[contains(name(),‘ui:’)]”, selectorsNamespaceManager)

Hi @ClaytonM,

many thanks for your suggestions, I think you are really among the most knoledgeable RPA developers in this forum. Concerning your code diagramTree.DocumentElement.SelectNodes("//*[contains(Name(),'Sequence')]", sequenceNamespaceManager), I would like to adjust it to find not those selectors called “sequence” (which is what your code does) but instead those selectors whose DISPLAY NAME contains a certain keyword.

How can I do this? This would really be the key to complete my application.

Thanks
Kind regards
Gennaro

Hi @ClaytonM

The code is almost done but I now have the following problem: the parent ID is not always correctly read. For example the parent ID of “ui:SendHotkey” is not 4 but is 3.

NodesAnalysisRecursiveNew.zip (14.5 KB)

What can it be?

Many thanks
Gennaro