44

I have a list of XML files, from which I have to get the string after a particular line.

In the files, I need to look for a tag Event and get the attribute value DLLRoutine. e.g. the tag would look something like below ...

<Event Definition="Validate" DLLPath="" DLLName="Helper.dll" DLLClass="HelpMain" 
       DLLRoutine="pgFeatureInfoOnValidate_WriteToRegSelectedFeatures" 
       InputParameters="pTreeViewFeatureTreeServerOS" RunOnce="no"/>

I just need to get Dllroutine values. How to do it using PowerShell?

5 Answers 5

50

Assuming your XML structure is something similar to:

$xml = [xml]'
<Events>
<Event Definition="Validate" DLLPath="" DLLName="Helper.dll" DLLClass="HelpMain" DLLRoutine="pgFeatureInfoOnValidate_WriteToRegSelectedFeatures" InputParameters="pTreeViewFeatureTreeServerOS" RunOnce="no"/>
<Event Definition="Validate1" DLLPath="" DLLName="Helper.dll1" DLLClass="HelpMain1" DLLRoutine="pgFeatureInfoOnValidate_WriteToRegSelectedFeatures" InputParameters="pTreeViewFeatureTreeServerOS" RunOnce="no"/>
</Events>
'

#Or get it from a XML file
$xml = [xml](Get-Content $XMLPath)

$xml.Events.Event | Select DLLName
Sign up to request clarification or add additional context in comments.

2 Comments

Great, in addition, I use ` -ExpandProperty Name` to get only value, without headings: $xml.Events.Event | Select DLLName -ExpandProperty Name, found here: stackoverflow.com/a/25184592/11159476
@gluttony For the select you need to specify the property only once: "| Select -ExpandProperty DLLName"
23

Assuming your Event element has an Events element root:

$xml.Events.Event.DLLName

I've only tested this in Powershell 3

Comments

19

you can use also xpath instead of dot notation:

$xml.SelectNodes('//Events/Event') | select DLLName

1 Comment

This approach worked really good when trying to parse nodes with special characters on the name.
6

You could use Select-XML:

$xml = [xml]'
<Events>
<Event Definition="Validate" DLLPath="" DLLName="Helper.dll" DLLClass="HelpMain" DLLRoutine="pgFeatureInfoOnValidate_WriteToRegSelectedFeatures" InputParameters="pTreeViewFeatureTreeServerOS" RunOnce="no"/>
<Event Definition="Validate1" DLLPath="" DLLName="Helper.dll1" DLLClass="HelpMain1" DLLRoutine="pgFeatureInfoOnValidate_WriteToRegSelectedFeatures" InputParameters="pTreeViewFeatureTreeServerOS" RunOnce="no"/>
</Events>
'
($Select-XML -xml $xml -xpath "//Event/@DLLName").Node

2 Comments

Did you try this? When I attempt in Powershell 5 it just returns a bunch of node and object metadata, not the actual contents.
Yep. You are getting the results you should be. PowerShell is designed around objects. Try this to get the DLL names: (Select-XML -xml $xml -xpath "//Event/@DLLName").Node
3

Try $xml.Events.Event.DLLName

It will work in version 2, I have tried to use xpath in several scripts and it fails every time so until I tried the dot notation I thought I was doing it wrong.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.