Monday, 28 June 2010

Remove-XMLAttributeAt.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script removed an an attribute from an XML element. 
  4. .DESCRIPTION 
  5.     This script creates an XML document, then removes an 
  6.     Attribute using the RemoveAttributeAt method. 
  7. .NOTES 
  8.     File Name  : Remove-XMLAttributeAt.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell Version 2.0 
  11. .LINK 
  12.     This script posted to: 
  13.         http://www.pshscripts.blogspot.com  
  14.     MSDN Sample posted at: 
  15.         http://msdn.microsoft.com/en-us/library/system.xml.xmlelement.removeattributeat.aspx  
  16. .EXAMPLE 
  17.     PSH [C:\foo]: . 'E:\PowerShellScriptLib\System.XML\Remove-XMLAttrivuteAt.ps1' 
  18.     Display the initial XML... 
  19.     <book genre="novel" ISBN="1-861001-57-5"><title>Pride And Prejudice</title></book> 
  20.     Display the modified XML... 
  21.     <book ISBN="1-861001-57-5"><title>Pride And Prejudice</title></book> 
  22. #> 
  23.  
  24. # Create and load an XML Document 
  25. $Doc = New-Object System.Xml.XmlDocument 
  26. $Doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>"
  27.                 "<title>Pride And Prejudice</title>"
  28.                 "</book>"
  29. # Set root 
  30. $Root = $Doc.DocumentElement 
  31.  
  32. # Display initial XML 
  33. "Display the initial XML..." 
  34. $Doc.InnerXml 
  35.  
  36. # Remove the genre attribute 
  37. $Return = $Root.RemoveAttributeAt(0) 
  38.  
  39. # Display result 
  40. "Display the modified XML..." 
  41. $Doc.InnerXml 

No comments: