Wednesday, 30 June 2010

Set-XMLAttribute.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script adds an attribute to an XML node 
  4. .DESCRIPTION 
  5.     This script creates a simple XML document and sets an attribute 
  6.     on a node. The script displays the before and after results, and 
  7.     is a conversion and extension of the MSDN sample.  
  8. .NOTES 
  9.     File Name  : Set-XMLAttribute.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12. .LINK 
  13.     This script posted to: http://www.pshscripts.blogspot.com 
  14.     MSDN Sample posted at: http://msdn.microsoft.com/en-us/library/y1ah1zbw.aspx  
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Set-XMLAttribute.ps1 
  17.     Display the Starting XML... 
  18.     <book xmlns:bk="urn:samples" bk:ISBN="1-861001-57-5"><title>Pride And Prejudice</title></book> 
  19.     Display the modified XML... 
  20.     <book xmlns:bk="urn:samples" bk:ISBN="1-861001-57-5" genre="novel"><title>Pride And Prejudice</title></book> 
  21. #> 
  22.  
  23. # Create the XML document and populate 
  24.   
  25. $Doc = New-Object System.XML.XmlDocument 
  26. $Doc.LoadXml("<book xmlns:bk='urn:samples' bk:ISBN='1-861001-57-5'>"
  27. "<title>Pride And Prejudice</title>"
  28. "</book>"
  29. $Root = $doc.DocumentElement 
  30.   
  31. # Display the XML 
  32. "Display the Starting XML..." 
  33. $doc.InnerXml 
  34.   
  35. # Add an attribute and display results 
  36. $root.SetAttribute("genre", "novel"
  37. "Display the modified XML..." 
  38. $doc.InnerXmL 

No comments: