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?
One way to read an XML document in PowerShell is to typecast a variable to the type [xml]. To create this variable, we can use the Get-Content cmdlet to read all of the text in an XML document. To typecast the output of Get-Content we can simply prepend the text [xml] before the variable.
Another way to use PowerShell to parse XML is to convert that XML to objects. The easiest way to do this is with the [xml] type accelerator. By prefixing the variable names with [xml] , PowerShell converts the original plain text XML into objects you can then work with.
The Attr object represents an attribute of an Element object. The allowable values for attributes are usually defined in a DTD. Because the Attr object is also a Node, it inherits the Node object's properties and methods.
Collect properties of all Windows Services on the local machine using Get-Service CmdLet. Pipeline the result from Get-Service CmdLet to the Export-Clixml CmdLet that will save resultset as an XML file. Import XML file into $ImportXML variable using Import-Clixml CmdLet. Show the resultset in the PowerShell Grid.
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
Assuming your Event
element has an Events
element root:
$xml.Events.Event.DLLName
I've only tested this in Powershell 3
you can use also xpath instead of dot notation:
$xml.SelectNodes('//Events/Event') | select DLLName
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With