Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch an attribute value from xml using powershell?

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?

like image 703
Samselvaprabu Avatar asked Aug 31 '12 09:08

Samselvaprabu


People also ask

How do I read XML attributes in 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.

Can PowerShell parse XML?

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.

What is attr in XML?

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.

How do I import XML into PowerShell?

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.


4 Answers

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
like image 160
ravikanth Avatar answered Oct 21 '22 20:10

ravikanth


Assuming your Event element has an Events element root:

$xml.Events.Event.DLLName

I've only tested this in Powershell 3

like image 19
WhiteKnight Avatar answered Oct 21 '22 21:10

WhiteKnight


you can use also xpath instead of dot notation:

$xml.SelectNodes('//Events/Event') | select DLLName
like image 17
walid2mi Avatar answered Oct 21 '22 21:10

walid2mi


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"
like image 8
pim Avatar answered Oct 21 '22 20:10

pim