{"id":4981,"date":"2012-09-13T00:01:00","date_gmt":"2012-09-13T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/09\/13\/use-powershell-to-simplify-access-to-xml-data\/"},"modified":"2012-09-13T00:01:00","modified_gmt":"2012-09-13T00:01:00","slug":"use-powershell-to-simplify-access-to-xml-data","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-simplify-access-to-xml-data\/","title":{"rendered":"Use PowerShell to Simplify Access to XML Data"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell&nbsp;3.0 to read XML files.<\/p>\n<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Tomorrow, we have the speaker&rsquo;s dinner at the house that script built. The Scripting Wife and I have invited all the Windows PowerShell Saturday speakers for the&nbsp;<a href=\"http:\/\/powershellsaturday.com\/002\/\" target=\"_blank\">Charlotte Windows PowerShell Saturday<\/a>&nbsp;event to our house for a cook-out. We are also having a Windows PowerShell slumber party. It will be cool&mdash;sitting around a camp fire, making <a href=\"http:\/\/www.wikihow.com\/Make-a-S%27more\" target=\"_blank\">s&#8217;mores<\/a>, writing Windows PowerShell scripts. Dude!<\/p>\n<\/p>\n<h2>First create an XML document<\/h2>\n<\/p>\n<p>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 <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.xml.xmldocument.aspx\" target=\"_blank\">XmlDocument class<\/a> is documented on MSDN. To obtain an instance of the <b>XmlDocument <\/b>class, use the <b>Get-Content<\/b> cmdlet to read the XML file, and then cast the content to XML. This is shown here.<\/p>\n<\/p>\n<p style=\"padding-left: 30px\">[xml]$books = Get-Content C:\\fso\\Books.XML<\/p>\n<\/p>\n<p><b>Note&nbsp;&nbsp;&nbsp;<\/b>To practice working with an XML document, it is important to have a well-formed XML document. You can obtain a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/ms762271(v=vs.85).aspx\" target=\"_blank\">sample XML document<\/a> on MSDN.<\/p>\n<\/p>\n<p>When working with XML, I like to use <a href=\"http:\/\/www.microsoft.com\/en-us\/download\/details.aspx?displaylang=en&amp;id=7973\" target=\"_blank\">XML Notepad<\/a>. 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&nbsp;8, so I guess an update is not really needed).<\/p>\n<\/p>\n<p>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.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4034.hsg-9-13-12-1.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4034.hsg-9-13-12-1.png\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<\/p>\n<h2>Use dotted notation to walk through the nodes<\/h2>\n<\/p>\n<p>After you have an XML document, it is easy to walk through the nodes. This is especially true with Windows PowerShell&nbsp;3.0 because it has the &ldquo;automatic foreach&rdquo; feature that I talked about in <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/08\/20\/my-five-favorite-powershell-3-0-tips-and-tricks.aspx\" target=\"_blank\">My Five Favorite PowerShell&nbsp;3.0 Tips and Tricks<\/a>.<\/p>\n<\/p>\n<p>After you use <b>Get-Content<\/b> to read the content of the XML file, you can use the <b>GetType<\/b><i> <\/i>method to see that it is an XMLDOCUMENT that returns. This is shown here.<\/p>\n<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; [xml]$books = Get-Content C:\\fso\\Books.XML<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">PS C:\\&gt; $books.GetType()<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">IsPublic IsSerial Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BaseType<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&#8212;&#8212;&#8211; &#8212;&#8212;&#8211; &#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">True&nbsp;&nbsp;&nbsp;&nbsp; False&nbsp;&nbsp;&nbsp; XmlDocument&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Xml.XmlNode<\/p>\n<\/p>\n<p>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.<\/p>\n<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $books.catalog.book.author<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">Gambardella, Matthew<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">Ralls, Kim<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">Corets, Eva<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">Corets, Eva<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">Corets, Eva<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">Randall, Cynthia<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">Thurman, Paula<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">Knorr, Stefan<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">Kress, Peter<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">O&#8217;Brien, Tim<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">O&#8217;Brien, Tim<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">Galos, Mike<\/p>\n<\/p>\n<p>To find all of the titles, use the<strong> t<\/strong><b>itle<\/b><i> <\/i>note as shown here.<\/p>\n<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $books.catalog.book.title<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">XML Developer&#8217;s Guide<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">Midnight Rain<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">Maeve Ascendant<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">Oberon&#8217;s Legacy<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">The Sundered Grail<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">Lover Birds<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">Splish Splash<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">Creepy Crawlies<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">Paradox Lost<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">Microsoft .NET: The Programming Bible<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">MSXML3: A Comprehensive Guide<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">Visual Studio 7: A Comprehensive Guide<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">PS C:\\&gt;<\/p>\n<\/p>\n<p>Keep in mind that Tab expansion does not work for the nodes under <i>book. <\/i>To see the available properties (nodes) without using XML Notepad, pipe the results to <b>Get-Member<\/b>. This is shown here.<\/p>\n<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $books.catalog.book | gm -MemberType Property<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; TypeName: System.Xml.XmlElement<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MemberType Definition<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">author&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; string author {get;set;}<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">description&nbsp; Property&nbsp;&nbsp; string description {get;set;}<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">genre&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; string genre {get;set;}<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; string id {get;set;}<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">price&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; string price {get;set;}<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">publish_date Property&nbsp;&nbsp; string publish_date {get;set;}<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">title&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; string title {get;set;}<\/p>\n<\/p>\n<p>Well, that is about it for today. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.<\/p>\n<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell&nbsp;3.0 to read XML files. Microsoft Scripting Guy, Ed Wilson, is here. Tomorrow, we have the speaker&rsquo;s dinner at the house that script built. The Scripting Wife and I have invited all the Windows PowerShell Saturday speakers for the&nbsp;Charlotte Windows PowerShell Saturday&nbsp;event to our [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[364,3,4,45,165],"class_list":["post-4981","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-powershell-3-0","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell","tag-xml"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell&nbsp;3.0 to read XML files. Microsoft Scripting Guy, Ed Wilson, is here. Tomorrow, we have the speaker&rsquo;s dinner at the house that script built. The Scripting Wife and I have invited all the Windows PowerShell Saturday speakers for the&nbsp;Charlotte Windows PowerShell Saturday&nbsp;event to our [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4981","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=4981"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4981\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=4981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}