- <#
- .SYNOPSIS
- This script adds an attribute to an XML node
- .DESCRIPTION
- This script creates a simple XML document and sets an attribute
- on a node. The script displays the before and after results, and
- is a conversion and extension of the MSDN sample.
- .NOTES
- File Name : Set-XMLAttribute.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to: http://www.pshscripts.blogspot.com
- MSDN Sample posted at: http://msdn.microsoft.com/en-us/library/y1ah1zbw.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Set-XMLAttribute.ps1
- Display the Starting XML...
- <book xmlns:bk="urn:samples" bk:ISBN="1-861001-57-5"><title>Pride And Prejudice</title></book>
- Display the modified XML...
- <book xmlns:bk="urn:samples" bk:ISBN="1-861001-57-5" genre="novel"><title>Pride And Prejudice</title></book>
- #>
- # Create the XML document and populate
- $Doc = New-Object System.XML.XmlDocument
- $Doc.LoadXml("<book xmlns:bk='urn:samples' bk:ISBN='1-861001-57-5'>" +
- "<title>Pride And Prejudice</title>" +
- "</book>")
- $Root = $doc.DocumentElement
- # Display the XML
- "Display the Starting XML..."
- $doc.InnerXml
- # Add an attribute and display results
- $root.SetAttribute("genre", "novel")
- "Display the modified XML..."
- $doc.InnerXmL
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts
Wednesday, 30 June 2010
Set-XMLAttribute.ps1
Sunday, 27 June 2010
Get-XMLAttribute.ps1
- <#
- .SYNOPSIS
- This script retrieves an attribute from an XML Element
- .DESCRIPTION
- This script first creates an in-memory XML document, then used the
- HasAttribute and GetAttribute to retrieve an attribute.
- .NOTES
- File Name : Get-XMLAttribute.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/06/get-xmlattributeps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/efsc3w6k.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-XMLAttribute.ps1
- Elemnt has genre: novel
- #>
- # Create XML document in memory
- $doc = new-object System.Xml.XmlDocument
- $doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
- "<title>Pride And Prejudice</title>" +
- "</book>");
- # Set root
- $root = $doc.DocumentElement;
- # Check to see if the element has a genre attribute
- # Then display it
- if ($root.HasAttribute("genre")){
- $genre = $root.GetAttribute("genre");
- "Elemnt has genre: {0}" -f $genre
- }
Labels:
PowerShell scripts,
System.XML.XMLDocument,
xml
Tuesday, 19 May 2009
Get-XMLFileContents.ps1
- <#
- .SYNOPSIS
- This script reads a simple XML file and prints the results
- .DESCRIPTION
- This script is a re-write of an XML C# MSDN Sample.
- .NOTES
- File Name : get-xmlfilecontents.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- Books3.xml =
- <book>
- <title>C# Introduction</title>
- <price>20</price>
- </book>
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/t9bfea29.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-XMLFileContents.PS1
- The content of the title element: C# Introduction
- The content of the price element: 20
- #>
- ##
- # Start of Script
- ##
- # Create XML Reader
- $reader = [system.Xml.XmlReader]::Create("C:\foo\book3.xml")
- # Parse the XML document. ReadString is used to
- # read the text content of the elements.
- $result=$reader.Read()
- # Read/Display title
- $reader.ReadStartElement("book")
- $reader.ReadStartElement("title")
- "The content of the title element: {0}" -f $reader.ReadString()
- $reader.ReadEndElement()
- # Read and display price
- $reader.ReadStartElement("price")
- "The content of the price element: {0}" -f $reader.ReadString()
- $reader.ReadEndElement()
- $reader.ReadEndElement()
- # End of Script
Labels:
powershell,
PowerShell scripts,
system.xml.xmlreader,
xml
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
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>
Labels:
powershell,
PowerShell scripts,
rss,
xml
Subscribe to:
Posts (Atom)