{"id":52003,"date":"2009-11-16T00:01:00","date_gmt":"2009-11-16T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/11\/16\/hey-scripting-guy-is-there-an-easier-way-to-work-with-xml-files\/"},"modified":"2009-11-16T00:01:00","modified_gmt":"2009-11-16T00:01:00","slug":"hey-scripting-guy-is-there-an-easier-way-to-work-with-xml-files","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-is-there-an-easier-way-to-work-with-xml-files\/","title":{"rendered":"Hey, Scripting Guy! Is There an Easier Way to Work with XML Files?"},"content":{"rendered":"<p><!-- AddThis Button BEGIN --><a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/a><!-- AddThis Button END --><\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"><\/p>\n<p class=\"MsoNormal\">Hey, Scripting Guy! I have a file that I need to parse. It is an XML file, and it is very ugly. Who in the world ever thought that an XML file would be better than a plain CSV file? Unfortunately, Microsoft seems to be enamored with XML, and I cannot get away from it. Surely, there must be a better way to work with XML than to go cross-eyed looking at all those slashes and angle brackets! Can you throw a fellow a bone?<\/p>\n<p class=\"MsoNormal\">&#8212; TS<\/p>\n<p class=\"MsoNormal\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\">Hello TS, <\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. It is Friday in <a href=\"http:\/\/en.wikipedia.org\/wiki\/Charlotte,_North_Carolina\"><font face=\"Segoe\">Charlotte, North Carolina<\/font><\/a>, in the United States. There was <a href=\"http:\/\/en.wikipedia.org\/wiki\/Frost\"><font face=\"Segoe\">frost<\/font><\/a> on the lawn this morning, and I was tempted to make a mug of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Hot_chocolate\"><font face=\"Segoe\">hot chocolate<\/font><\/a> and forego my usual pot of English Breakfast tea, but I prefer real hot chocolate (not the powdered stuff) and I did not feel like fooling with the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Double_boiler\"><font face=\"Segoe\">double boiler<\/font><\/a> on a cold November morning. (I use milk, cream, whole vanilla beans, cinnamon sticks, almonds, hazelnuts, honey, and chunks of Mexican chocolate when I make hot chocolate so it is a big production.) So I ate my omelet while sipping my hot tea and checking my e-mail on my Windows Mobile 6.5 smart phone, and then headed up to my office primed and ready for a great day. It is after all no-meetings Friday. <\/p>\n<p class=\"MsoNormal\">TS, I am sincerely sorry that XML files upset you (my feelings were similar when I first saw XML). If you were here, I would fix you a cup of my special &ldquo;Scripting Guy Hot Chocolate.&rdquo; We would sit down, and I would show you that with Windows PowerShell 2.0, XML files need not be a hassle. Because you are not here, I suggest you make yourself some hot chocolate, and open up Windows PowerShell 2.0, and we&rsquo;ll work through this together. <\/p>\n<p class=\"MsoNormal\">One thing that makes working with XML documents easier is having the right tool. If you double-click the XML file, the default association is Internet Explorer. This view lets you open and close nodes by clicking the minus signs on the left side of the code, as seen here:<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of viewing the XML file in Internet Explorer\" alt=\"Image of viewing the XML file in Internet Explorer\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/november\/hey1116\/hsg-11-16-09-01.jpg\" width=\"600\" height=\"420\"><\/p>\n<p class=\"MsoNormal\"><br>Using Internet Explorer to view an XML file is better than using Notepad. The color highlighting makes it easier to find nodes, and you can hide nodes you are not interested in seeing by clicking the minus (<b>&#8211;<\/b>) symbol to the left of the node. There is a better tool to use; it is the XML Notepad. The XML Notepad is freely downloadable from the Microsoft <a href=\"http:\/\/www.microsoft.com\/downloads\/details.aspx?displaylang=en&amp;FamilyID=72d6aa49-787d-4118-ba5f-4f30fe913628\"><font face=\"Segoe\">Download Center<\/font><\/a>. Using the XML Notepad, you can easily figure out the structure of your XML file and find the names of the various nodes. This is shown here:<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of viewing the same file with XML Notepad\" alt=\"Image of viewing the same file with XML Notepad\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/november\/hey1116\/hsg-11-16-09-02.jpg\" width=\"600\" height=\"461\"><\/p>\n<p class=\"MsoNormal\"><br>The books.xml file that is loaded into XML Notepad, seen in the previous image, is included in various software development kits (SDKs), but you do not need to download and install an entire SDK just to gain access to this rather small XML file. You can easily create it by <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms762271(VS.85).aspx\"><font face=\"Segoe\">copying it from MSDN<\/font><\/a>, pasting it into Notepad, and saving it as books.xml (You can use normal Notepad or XML Notepad to do this, whichever one you prefer to use.) It is useful to have a well-formed XML file upon which to practice your techniques because if you run into problems, you will at least know the XML file is working properly. <\/p>\n<p class=\"MsoNormal\">The easiest way to read an XML file is to use the <b>Select-XML<\/b> cmdlet. You can use the <b>&ndash;path<\/b> parameter to point to an XML file, and the <b>&ndash;Xpath<\/b> parameter to provide the query. The command and associated results are shown here: <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Select-Xml -Path C:fsobooks.xml -XPath &#8220;\/\/title&#8221;<\/p>\n<p>Node<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>C:fsobooks.xml<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>\/\/title<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">One of the problems with the output above is that it is useless. If you examine the output, it lists title, the file name, and the search criteria; however, it provides no detail about the title, which is the information we were really interested in obtaining in the first place. To obtain the actual title of the book, you can pipe the results to the <b>Select-Object<\/b> cmdlet. However, before doing that, you need to know which kind of object you are working with, so that you will know which properties are available to select. To do this, you use the <b>Get-Member<\/b> cmdlet and choose the property <b>MemberType<\/b>. The <b>Get-Member<\/b> command and the resulting properties of the <b>SelectXMLInfo<\/b> object are in this code list: <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Select-Xml -Path C:fsobooks.xml -XPath &#8220;\/\/title&#8221; | Get-Member -MemberType property<\/p>\n<p><span>&nbsp;&nbsp; <\/span>TypeName: Microsoft.PowerShell.Commands.SelectXmlInfo<\/p>\n<p>Name<span>&nbsp;&nbsp;&nbsp; <\/span>MemberType Definition<br>&#8212;-<span>&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<br>Node<span>&nbsp;&nbsp;&nbsp; <\/span>Property<span>&nbsp;&nbsp; <\/span>System.Xml.XmlNode Node {get;set;}<br>Path<span>&nbsp;&nbsp;&nbsp; <\/span>Property<span>&nbsp;&nbsp; <\/span>System.String Path {get;set;}<br>Pattern Property<span>&nbsp;&nbsp; <\/span>System.String Pattern {get;set;}<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The <b>SelectXMLInfo<\/b> object only exposes three properties. From our earlier output, TS, you know that the path refers to the path to the XML file, and the pattern refers to the <b>XPath<\/b> query; therefore, the node must be the title information. You can use the <b>Select-Object<\/b> cmdlet to expand the <b>node<\/b> property. The revised Windows PowerShell command is contained in the code here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Select-Xml -Path C:fsobooks.xml -XPath &#8220;\/\/title&#8221; | Select-Object -ExpandProperty node<\/p>\n<p>#text<br>&#8212;&#8211;<br>XML Developer&#8217;s Guide<br>Midnight Rain<br>Maeve Ascendant<br>Oberon&#8217;s Legacy<br>The Sundered Grail<br>Lover Birds<br>Splish Splash<br>Creepy Crawlies<br>Paradox Lost<br>Microsoft .NET: The Programming Bible<br>MSXML3: A Comprehensive Guide<br>Visual Studio 7: A Comprehensive Guide<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">TS, you might be wondering what the <b>XPath<\/b> parameter is all about. <b>XPath<\/b> is a way of querying XML files. There are numerous books written about XPath, and hundreds of pages on MSDN, but a good starting point is to take a look a look at the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa926473.aspx\"><font face=\"Segoe\">syntax documentation on MSDN<\/font><\/a>. From this page, you can see that the <b>\/\/title<\/b> query I used told the <b>Select-Xml<\/b> cmdlet to look for the use of title anywhere within the XML document. If you want to return the name of the root node, you can use the <b>&ldquo;\/*&rdquo;<\/b> syntax seen in the listing here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Select-Xml -Path C:fsobooks.xml -XPath &#8220;\/*&#8221;<\/p>\n<p>Node<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>\/*<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">To find a book with the <b>id<\/b> attribute that is equal to <b>bk101<\/b>, you use the <b>\/\/<\/b> to find all of the books, and then use a set of square brackets to indicate you are looking for an attribute that is equal to a certain value. This command is in the code that follows. <\/p>\n<p class=\"Readeraidonly\">Keep in mind that XML values and attribute names are case sensitive. For example, book must begin with a small &ldquo;b&rdquo;, and &ldquo;id&rdquo; is lower case, as in &ldquo;bk101.&rdquo;<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Select-Xml -Path C:fsobooks.xml -XPath &#8220;\/\/book[@id = &#8216;bk101&#8217;]&#8221;<\/p>\n<p>Node<span>&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; <\/span>Path<span>&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; <\/span>Pattern<br>&#8212;-<span>&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; <\/span>&#8212;-<span>&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; <\/span>&#8212;&#8212;-<br>book<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&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;<\/span>C:fsobooks.xml<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>\/\/book[@id = &#8216;bk101&#8217;]<\/p>\n<p><br>PS C:&gt;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Well, TS, as you can see, working with XML files does not need to be complicated. Join us tomorrow as XML Week continues. <\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\"><font face=\"Segoe\">Facebook<\/font><\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a> or post them on the <a href=\"http:\/\/bit.ly\/hsgforum\" target=\"_blank\"><font face=\"Segoe\">Official Scripting Guys Forum<\/font><\/a>. See you tomorrow. Until then, peace.<br>&nbsp;<\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/p>\n<p><\/span><\/b><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Hey, Scripting Guy! I have a file that I need to parse. It is an XML file, and it is very ugly. Who in the world ever thought that an XML file would be better than a plain CSV file? Unfortunately, Microsoft seems to be enamored with XML, and I cannot get away from [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,45,165],"class_list":["post-52003","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell","tag-xml"],"acf":[],"blog_post_summary":"<p>&nbsp; Hey, Scripting Guy! I have a file that I need to parse. It is an XML file, and it is very ugly. Who in the world ever thought that an XML file would be better than a plain CSV file? Unfortunately, Microsoft seems to be enamored with XML, and I cannot get away from [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/52003","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=52003"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/52003\/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=52003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=52003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=52003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}