Use PowerShell to Simplify Access to XML Data

Use PowerShell to Simplify Access to XML Data

  • Comments 2
  • Likes

Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell 3.0 to read XML files.

Microsoft Scripting Guy, Ed Wilson, is here. Tomorrow, we have the speaker’s dinner at the house that script built. The Scripting Wife and I have invited all the Windows PowerShell Saturday speakers for the Charlotte Windows PowerShell Saturday event to our house for a cook-out. We are also having a Windows PowerShell slumber party. It will be cool—sitting around a camp fire, making s'mores, writing Windows PowerShell scripts. Dude!

First create an XML document

The first thing to do is to create an instance of the XML document class. This is really easy by using the [XML] type accelerator. The XmlDocument class is documented on MSDN. To obtain an instance of the XmlDocument class, use the Get-Content cmdlet to read the XML file, and then cast the content to XML. This is shown here.

[xml]$books = Get-Content C:\fso\Books.XML

Note   To practice working with an XML document, it is important to have a well-formed XML document. You can obtain a sample XML document on MSDN.

When working with XML, I like to use XML Notepad. It is a free download from the Microsoft Download Center. (Unfortunately, it has not been updated since 2007, but it installs and works great on Windows 8, so I guess an update is not really needed).

The Books.XML XML document in XML Notepad is shown in the following image. This is a great way to explore an XML document prior to using Windows PowerShell to explore the document.

Image of menu

Use dotted notation to walk through the nodes

After you have an XML document, it is easy to walk through the nodes. This is especially true with Windows PowerShell 3.0 because it has the “automatic foreach” feature that I talked about in My Five Favorite PowerShell 3.0 Tips and Tricks.

After you use Get-Content to read the content of the XML file, you can use the GetType method to see that it is an XMLDOCUMENT that returns. This is shown here.

PS C:\> [xml]$books = Get-Content C:\fso\Books.XML

PS C:\> $books.GetType()

 

IsPublic IsSerial Name                                     BaseType

-------- -------- ----                                     --------

True     False    XmlDocument                              System.Xml.XmlNode

In the previous image, there is the catalog node, a book node, and each book has an author node. By using the dotted notation and the automatic foreach, the authors are easily accessible as shown here.

PS C:\> $books.catalog.book.author

Gambardella, Matthew

Ralls, Kim

Corets, Eva

Corets, Eva

Corets, Eva

Randall, Cynthia

Thurman, Paula

Knorr, Stefan

Kress, Peter

O'Brien, Tim

O'Brien, Tim

Galos, Mike

To find all of the titles, use the title note as shown here.

PS C:\> $books.catalog.book.title

XML Developer's Guide

Midnight Rain

Maeve Ascendant

Oberon's Legacy

The Sundered Grail

Lover Birds

Splish Splash

Creepy Crawlies

Paradox Lost

Microsoft .NET: The Programming Bible

MSXML3: A Comprehensive Guide

Visual Studio 7: A Comprehensive Guide

PS C:\>

Keep in mind that Tab expansion does not work for the nodes under book. To see the available properties (nodes) without using XML Notepad, pipe the results to Get-Member. This is shown here.

PS C:\> $books.catalog.book | gm -MemberType Property

 

   TypeName: System.Xml.XmlElement

 

Name         MemberType Definition

----         ---------- ----------

author       Property   string author {get;set;}

description  Property   string description {get;set;}

genre        Property   string genre {get;set;}

id           Property   string id {get;set;}

price        Property   string price {get;set;}

publish_date Property   string publish_date {get;set;}

title        Property   string title {get;set;}

Well, that is about it for today. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Thanks for all the helpful info here!

    After reading the whole XML file using -- [xml]$books = Get-Content C:\fso\Books.XML --, is there a way to change some of the data and then write it back into the XML file? I tried  -- Set-Content C:\fso\Books.XML -value $books -- but I guess I have to use on of the $books.<??> properties or methods.

    Rgds, Matthy

  • Thanks very much for the valuable information, How can we get get data from xml document if there multiple namespaces in the document.