Reading XML Content

by Mar 23, 2016

Reading XML formatted text is easy when you use an XML object for it. With its method Load(), you can read in content from a file path or a URL.

In a previous tip we showed how to create a new XML file from scratch. This XML document is used in the next example. If you missed the tip, you find the content of the XML file below. Make sure you save it into $env:temp\inventory.xml.

To read an XML file, load it into the XML object. Then, you simply specify the nodes you want to traverse. To get a list of all inventory machines, just use $xml.Machines.Machine:

# this is where the XML sample file was saved
$Path = "$env:temp\inventory.xml"
 
# load it into an XML object
$xml = New-Object -TypeName XML
$xml.Load($Path)
# note: if your XML is malformed, you will get an exception here
# always make sure your node names do not contain spaces
 
# simply traverse the nodes and select the information you want
$Xml.Machines.Machine | Select-Object -Property Name, IP

Here is the sample XML document:

<?xml version="1.0"?>
<?xml-stylesheet type='text/xsl' href='style.xsl'?>
<!–List of machines–>
<Machines current="True" manager="Tobias">
          <!–1. machine details–>
          <Machine test="940854388">
                   <Name>Server0001</Name>
                   <IP>40.101.105.27</IP>
                   <GUID>8c49e799-b661-4076-b325-f030d25b299e</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
          <!–2. machine details–>
          <Machine test="1567577618">
                   <Name>Server0002</Name>
                   <IP>161.29.237.75</IP>
                   <GUID>eca864a0-28a6-4e60-9438-c58b3f4c5b91</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
          <!–3. machine details–>
          <Machine test="1385488736">
                   <Name>Server0003</Name>
                   <IP>202.222.193.169</IP>
                   <GUID>487b0b85-d03b-4fe1-bec1-895bb4c6f43d</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
          <!–4. machine details–>
          <Machine test="1141441915">
                   <Name>Server0004</Name>
                   <IP>115.249.246.167</IP>
                   <GUID>917a1243-c354-41d8-aa8e-5b9bf0334a80</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
          <!–5. machine details–>
          <Machine test="2134573729">
                   <Name>Server0005</Name>
                   <IP>239.97.156.231</IP>
                   <GUID>f961916d-ccfb-4395-8e61-59299ef11926</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
          <!–6. machine details–>
          <Machine test="1569812933">
                   <Name>Server0006</Name>
                   <IP>66.5.84.251</IP>
                   <GUID>154e5e92-f063-4571-8987-2d3e76b34331</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
          <!–7. machine details–>
          <Machine test="426025640">
                   <Name>Server0007</Name>
                   <IP>99.140.184.148</IP>
                   <GUID>0568c635-8bd9-4834-8084-28e1fd78d512</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
          <!–8. machine details–>
          <Machine test="1706184427">
                   <Name>Server0008</Name>
                   <IP>129.242.172.7</IP>
                   <GUID>b106eff3-5642-4857-a12d-53d6f552bf07</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
          <!–9. machine details–>
          <Machine test="1724935941">
                   <Name>Server0009</Name>
                   <IP>165.234.115.96</IP>
                   <GUID>e72b07ae-e5fa-49a9-a663-f17b4894c50d</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
          <!–10. machine details–>
          <Machine test="154244134">
                   <Name>Server0010</Name>
                   <IP>191.162.109.0</IP>
                   <GUID>f4568d8d-2302-404c-8c0e-1f7047cf8a1a</GUID>
                   <Information info1="some info" info2="more info">RawContent</Information>
                   <CodeSegment info3="another attribute"><![CDATA[this is untouched code and can contain special characters /\@<>]]></CodeSegment>
          </Machine>
</Machines>

Twitter This Tip! ReTweet this Tip!