I’ve been playing a bit this week with XML and PowerShell. As you no doubt know, PowerShell has first class XML support built in. To see more about that, see Tobias’s Ebook Chapter on XML and PowerShell. My task this week was to work with attributes that can appear inside an XML tag. I was using the .NET XML class System.XML.XMLElement and it’s various attribute related method.
As noted in MSDN, are a node in a DOM (XML) document. These elements can have attributes which you can associate with the element. For example, consider the following XML element:
<book genre='novel' ISBN='1-861001-57-5'>
<title>Pride And Prejudice</title>
</book>"
Such an element would normally be part of a much larger collection (et <books></books>), but for the purposes of playiing with element attributews, you can load it and then treated as an XML document with elements (albeit not many). You can load this document like this (and yes, there are a bunch more ways!)
$Doc = New-Object System.Xml.XmlDocument
$Doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
" <title>Pride And Prejudice</title>" +
"</book>" )
In the XML document, the book element has two attributes, genre and ISBN. Each attribute has the simple format (in the XML) of <attribute name>=<attributevalue>.
Once you load the document, you can do things like:
- Check whether an element has a particular named attribute
- Get the value of an attribute
- Remove an attribute
- Set and attribute
To do this in Powershell you would do something like this, eg to set an attribute:
$Root = $Doc.DocumentELement
$Root.SetAttribute("attributename","value")
In richer XML scripts the attributename and the value would be held in a variable (that you in turn might have obtained from another XML document).
I’ve written several sample scripts over on the PowerShell scripts blog, which re-implement a number of MSDN attribute handling C# samples:
- Get-XMLAttribute.ps1 – this script loads the XML then checks to see if the element has an attribute and if so, the code prints out the value of the attribute.
- Remove-XMLAttribute and Remove-XMLAttributeAt.ps1 – these scripts load the XML and then remove the attribute, but using different .NET methods (i.e. RemoveAttribute and RemoveAttributeAt). Using the Remove AttributeAt, where you specify the position of the attribute, and not the name, is potentially dangerous. I have the t-shirt on that one!
- Set-XMLAttribute.ps1 – this script as the name might imply, loads the XML and adds an attribute to the element.
Fun stuff!
No comments:
Post a Comment