Sunday 27 June 2010

Get-XMLAttribute.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script retrieves an attribute from an XML Element  
  4. .DESCRIPTION 
  5.     This script first creates an in-memory XML document, then used the  
  6.     HasAttribute and GetAttribute to retrieve an attribute. 
  7. .NOTES 
  8.     File Name  : Get-XMLAttribute.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell Version 2.0 
  11. .LINK 
  12.     This script posted to: 
  13.         http://pshscripts.blogspot.com/2010/06/get-xmlattributeps1.html 
  14.     MSDN Sample posted at: 
  15.         http://msdn.microsoft.com/en-us/library/efsc3w6k.aspx
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\Get-XMLAttribute.ps1 
  18.     Elemnt has genre: novel 
  19. #> 
  20. # Create XML document in memory 
  21. $doc = new-object System.Xml.XmlDocument 
  22. $doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>"
  23.                 "<title>Pride And Prejudice</title>"
  24.                 "</book>"); 
  25. # Set root 
  26. $root = $doc.DocumentElement; 
  27.  
  28. # Check to see if the element has a genre attribute 
  29. # Then display it 
  30. if ($root.HasAttribute("genre")){ 
  31.       $genre = $root.GetAttribute("genre"); 
  32.       "Elemnt has genre: {0}" -f $genre 
  33.    } 

No comments: