Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts

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 

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.    } 

Tuesday, 19 May 2009

Get-XMLFileContents.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script reads a simple XML file and prints the results 
  4. .DESCRIPTION 
  5.     This script is a re-write of an XML C# MSDN Sample. 
  6. .NOTES 
  7.     File Name  : get-xmlfilecontents.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell V2 CTP3 
  10.     Books3.xml =  
  11.       <book> 
  12.       <title>C# Introduction</title> 
  13.       <price>20</price> 
  14.       </book> 
  15. .LINK 
  16.     This script posted to: 
  17.         http://www.pshscripts.blogspot.com 
  18.     MSDN Sample posted at: 
  19.         http://msdn.microsoft.com/en-us/library/t9bfea29.aspx 
  20. .EXAMPLE 
  21.     PSH [C:\foo]: .\Get-XMLFileContents.PS1 
  22.     The content of the title element: C# Introduction 
  23.     The content of the price element: 20 
  24.   
  25. #> 
  26.   
  27. ## 
  28. # Start of Script 
  29. ## 
  30.   
  31. # Create XML Reader 
  32. $reader = [system.Xml.XmlReader]::Create("C:\foo\book3.xml") 
  33.   
  34. # Parse the XML document.  ReadString is used to  
  35. # read the text content of the elements. 
  36. $result=$reader.Read() 
  37.   
  38. # Read/Display title 
  39. $reader.ReadStartElement("book") 
  40. $reader.ReadStartElement("title")    
  41. "The content of the title element: {0}" -f $reader.ReadString() 
  42. $reader.ReadEndElement() 
  43.   
  44. # Read and display price 
  45. $reader.ReadStartElement("price") 
  46. "The content of the price element: {0}" -f $reader.ReadString() 
  47. $reader.ReadEndElement() 
  48. $reader.ReadEndElement() 
  49. # End of Script 

Sunday, 7 December 2008

Write-RSSStream.ps1

# Write-RSSStream.ps1
#
This sample shows how to use the XMLTextWriter
#
Based on Halr's content, but extended.
#
Thomas Lee - tfl@psp.co.uk

# Define output
$path = "c:\foo\file.xml"

# Create encoding, and create xml writer

$encoding = [System.Text.Encoding]::UTF8
$writer = New-Object System.Xml.XmlTextWriter( $Path, $encoding )
$writer.Formatting = [system.xml.formatting]::indented

# Write start of document
$writer.WriteStartDocument()

# Write Start of rss stream
$writer.WriteStartElement( "rss" )
$writer.WriteAttributeString( "version", "2.0" )

# write channel start info
$writer.WriteStartElement("channel")
$writer.WriteStartElement("title")
$writer.writestring("My Channel")
$writer.WriteEndElement() #end title
$Writer.WriteStartElement("Last Build Date")
$writer.WriteString((Get-Date).ToString())
$writer.WriteEndElement() #end last build date
$writer.WriteStartElement("subtitle")
$writer.WriteString("This Blog's subtitle")
$writer.WriteEndElement() #end subtitle

# Now here put an item in
$writer.WriteStartElement("item")
$writer.WriteStartElement("title")
$writer.WriteString("A cool RSS Feed Item")
$writer.WriteEndElement() #end title
$writer.WriteStartElement("link")
$writer.WriteString("http://pshscripts.blogspot.com")
$writer.WriteEndElement() #end link
$writer.WriteStartElement("description")
$writer.WriteString("This is the body of a blog post. It could go on, and on. ")
$writer.Writestring("Or not as the case may be!")
$writer.WriteEndElement() #end description
$writer.WriteStartElement("author")
$writer.WriteString("Thomas Lee - tfl@psp.co.uk")
$writer.WriteEndElement() #end author
$writer.WriteEndElement() #end item

# Write end of document information
$writer.WriteEndElement() # End Channel
$writer.WriteEndElement() #End RSS

# Make sure we close the file
$writer.close()

# Let's see what it has done
cat $path

This script produces the following output:

PS C:\foo> . 'C:\foo\write-rssstream.PS1'
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>My Channel</title>
<Last Build Date>12/5/2008 8:50:26 AM</Last Build Date>
<subtitle>This Blog's subtitle</subtitle>
<item>
<title>A cool RSS Feed Item</title>
<link>http://pshscripts.blogspot.com</link>
<description>This is the body of a blog post. It could go on, and on.
Or not
as the case may be!</description>
<author>Thomas Lee - tfl@psp.co.uk</author>
</item>
</channel>
</rss>