title: Parse an XML file with Powershell
body: <p>I'm trying to parse an xml file and can't seem to extract the pieces I want. The xml file is for a homegrown system and I don't have any control over its layout.</p>
  <p>The file looks like this –</p>
  <pre><code>$doc = [xml]@'
  &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;rbacx &gt;
      &lt;namespace namespaceName=&quot;Team Name&quot; namespaceShortName=&quot;ABC&quot;/&gt;
      &lt;attributeValues&gt;
          &lt;attributeValue id=&quot;Role=Administrator&quot;&gt;
              &lt;value&gt;&lt;![CDATA[Administrator]]&gt;&lt;/value&gt;
              &lt;attributes&gt;
                  &lt;attribute name=&quot;Glossary&quot;&gt;
                      &lt;attributeValues&gt;
                          &lt;attributeValue&gt;&lt;value&gt;&lt;![CDATA[Administrator (service accounts)]]&gt;&lt;/value&gt;&lt;/attributeValue&gt;
                      &lt;/attributeValues&gt;
                  &lt;/attribute&gt;
              &lt;/attributes&gt;
          &lt;/attributeValue&gt;
          &lt;attributeValue id=&quot;Role=Operator&quot;&gt;
              &lt;value&gt;&lt;![CDATA[Operator]]&gt;&lt;/value&gt;
              &lt;attributes&gt;
                  &lt;attribute name=&quot;Glossary&quot;&gt;
                      &lt;attributeValues&gt;
                          &lt;attributeValue&gt;&lt;value&gt;&lt;![CDATA[Operator (all accounts)]]&gt;&lt;/value&gt;&lt;/attributeValue&gt;
                      &lt;/attributeValues&gt;
                  &lt;/attribute&gt;
              &lt;/attributes&gt;
          &lt;/attributeValue&gt;
      &lt;/attributeValues&gt;
      &lt;accounts&gt;
          &lt;account id=&quot;FRED&quot;&gt;
              &lt;name&gt;&lt;![CDATA[FRED@xyz.com]]&gt;&lt;/name&gt;
              &lt;endPoint&gt;ABC&lt;/endPoint&gt;
              &lt;domain&gt;ABC&lt;/domain&gt;
              &lt;comments/&gt;
              &lt;attributes&gt;
                  &lt;attribute name=&quot;AppBoRID&quot;&gt;
                      &lt;attributeValues&gt;
                          &lt;attributeValue&gt;&lt;value&gt;&lt;![CDATA[FRED]]&gt;&lt;/value&gt;&lt;/attributeValue&gt;
                      &lt;/attributeValues&gt;
                  &lt;/attribute&gt;
                  &lt;attribute name=&quot;Role&quot;&gt;
                      &lt;attributeValues&gt;&lt;attributeValueRef id=&quot;Role=Operator&quot;/&gt;&lt;/attributeValues&gt;
                  &lt;/attribute&gt;
              &lt;/attributes&gt;
          &lt;/account&gt;
          &lt;account id=&quot;BARNEY&quot;&gt;
              &lt;name&gt;&lt;![CDATA[BARNEY@xyz.com]]&gt;&lt;/name&gt;
              &lt;endPoint&gt;ABC&lt;/endPoint&gt;
              &lt;domain&gt;ABC&lt;/domain&gt;
              &lt;comments/&gt;
              &lt;attributes&gt;
                  &lt;attribute name=&quot;AppBoRID&quot;&gt;
                      &lt;attributeValues&gt;
                          &lt;attributeValue&gt;&lt;value&gt;&lt;![CDATA[BARNEY]]&gt;&lt;/value&gt;&lt;/attributeValue&gt;
                      &lt;/attributeValues&gt;
                  &lt;/attribute&gt;
                  &lt;attribute name=&quot;Role&quot;&gt;
                      &lt;attributeValues&gt;&lt;attributeValueRef id=&quot;Role=Administrator&quot;/&gt;&lt;/attributeValues&gt;
                  &lt;/attribute&gt;
              &lt;/attributes&gt;
          &lt;/account&gt;
          &lt;account id=&quot;NonPeopleID_CC1234&quot;&gt;
              &lt;name&gt;&lt;![CDATA[WILMA@xyz.com]]&gt;&lt;/name&gt;
              &lt;endPoint&gt;ABC&lt;/endPoint&gt;
              &lt;domain&gt;ABC&lt;/domain&gt;
              &lt;comments/&gt;
              &lt;attributes&gt;
                  &lt;attribute name=&quot;appUserName&quot;&gt;
                      &lt;attributeValues&gt;
                          &lt;attributeValue&gt;&lt;value&gt;&lt;![CDATA[WILMA]]&gt;&lt;/value&gt;&lt;/attributeValue&gt;
                      &lt;/attributeValues&gt;
                  &lt;/attribute&gt;
                  &lt;attribute name=&quot;CostCentre&quot;&gt;
                      &lt;attributeValues&gt;
                          &lt;attributeValue&gt;&lt;value&gt;&lt;![CDATA[1234]]&gt;&lt;/value&gt;&lt;/attributeValue&gt;
                      &lt;/attributeValues&gt;
                  &lt;/attribute&gt;
                  &lt;attribute name=&quot;Bank_Number&quot;&gt;
                      &lt;attributeValues&gt;
                          &lt;attributeValue&gt;&lt;value&gt;&lt;![CDATA[0000]]&gt;&lt;/value&gt;&lt;/attributeValue&gt;
                      &lt;/attributeValues&gt;
                  &lt;/attribute&gt;
                  &lt;attribute name=&quot;Directory&quot;&gt;
                      &lt;attributeValues&gt;
                          &lt;attributeValue&gt;&lt;value&gt;&lt;![CDATA[XYZ]]&gt;&lt;/value&gt;&lt;/attributeValue&gt;
                      &lt;/attributeValues&gt;
                  &lt;/attribute&gt;
                  &lt;attribute name=&quot;Role&quot;&gt;
                      &lt;attributeValues&gt;&lt;attributeValueRef id=&quot;Role=Administrator&quot;/&gt;&lt;/attributeValues&gt;
                  &lt;/attribute&gt;
              &lt;/attributes&gt;
          &lt;/account&gt;
          &lt;account id=&quot;NonPeopleID_CC1234&quot;&gt;
              &lt;name&gt;&lt;![CDATA[BETTY@xyz.com]]&gt;&lt;/name&gt;
              &lt;endPoint&gt;ABC&lt;/endPoint&gt;
              &lt;domain&gt;ABC&lt;/domain&gt;
              &lt;comments/&gt;
              &lt;attributes&gt;
                  &lt;attribute name=&quot;appUserName&quot;&gt;
                      &lt;attributeValues&gt;
                          &lt;attributeValue&gt;&lt;value&gt;&lt;![CDATA[BETTY]]&gt;&lt;/value&gt;&lt;/attributeValue&gt;
                      &lt;/attributeValues&gt;
                  &lt;/attribute&gt;
                  &lt;attribute name=&quot;CostCentre&quot;&gt;
                      &lt;attributeValues&gt;
                          &lt;attributeValue&gt;&lt;value&gt;&lt;![CDATA[1234]]&gt;&lt;/value&gt;&lt;/attributeValue&gt;
                      &lt;/attributeValues&gt;
                  &lt;/attribute&gt;
                  &lt;attribute name=&quot;Bank_Number&quot;&gt;
                      &lt;attributeValues&gt;
                          &lt;attributeValue&gt;&lt;value&gt;&lt;![CDATA[0000]]&gt;&lt;/value&gt;&lt;/attributeValue&gt;
                      &lt;/attributeValues&gt;
                  &lt;/attribute&gt;
                  &lt;attribute name=&quot;Directory&quot;&gt;
                      &lt;attributeValues&gt;
                          &lt;attributeValue&gt;&lt;value&gt;&lt;![CDATA[XYZ]]&gt;&lt;/value&gt;&lt;/attributeValue&gt;
                      &lt;/attributeValues&gt;
                  &lt;/attribute&gt;
                  &lt;attribute name=&quot;Role&quot;&gt;
                      &lt;attributeValues&gt;&lt;attributeValueRef id=&quot;Role=Operator&quot;/&gt;&lt;/attributeValues&gt;
                  &lt;/attribute&gt;
              &lt;/attributes&gt;
          &lt;/account&gt;
      &lt;/accounts&gt;
  &lt;/rbacx&gt;
  
  '@
  </code></pre>
  <p>I'd like to get back output looking like this –</p>
  <pre><code>Name            Role
  FRED            Operator
  BARNEY          Administrator
  WILMA           Administrator
  BETTY           Operator
  
  </code></pre>
  <p>I'm able to get the name@domain with this –</p>
  <pre><code>$Doc.rbacx.accounts.account.name 
  
  where it returns - 
  
  #cdata-section
  --------------
  FRED@xyz.com  
  BARNEY@xyz.com
  WILMA@xyz.com 
  BETTY@xyz.com  
  </code></pre>
  <p>I can get all the value attributes with this -</p>
  <pre><code>$Doc.rbacx.accounts.account.attributes.attribute.attributevalues.attributevalue.value 
  
  where it returns - 
  
  #cdata-section
  --------------
  FRED          
  BARNEY        
  WILMA         
  1234          
  0000          
  XYZ           
  BETTY         
  1234          
  0000          
  XYZ   
  </code></pre>
  <p>I can't seem to get the role associated with the user returned. I'm thinking it should be along the lines of this –</p>
  <pre><code>$Doc.rbacx.accounts.account.attributes.attribute.attributevalues.attributevalue.attributeValueRef 
  </code></pre>
  <p>However that doesn't return anything.</p>
  <p>Any thoughts on how I can get the User &amp; its associated Role output here?</p>