Read xml file attributes using powershell

I am trying to read a XML with powershell. According to the value of Name attribute in <Resource> tag, I should retrieve the value of ResourceUri attribute in < <Uri> tag.

Sample XML is given below. I tried the following. But I am able to achieve it.

If Name in <Resource> tag is XYZ then the ResouceUri value = http://ustfinder.com/ExternalApp.aspx/NewApp?CRWAAMONITOR should be retrived.

$var = Select-Xml -Path "C:\Temp\Desktop\EIB Autaomation\abc.xml" -XPath "/MLIFRTCUI/Businesses/Business/Resource" | where { $_.Node.ParentNode.Name -eq "xyz"} | % {$_.Node.ResourceUri} | select -unique

Please help with this script.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

<MLIFRTCUI>
  <!-- DATE-TIME:  2015-10-06 14:13:01 (UTC) -->
  <!-- UI SERVER: MPHEW1VQSH001 -->
  <!-- SQL FQDN: ifrtcdbrw.pl1.us.ml.com -->
  <!-- SQL SERVER: MPHEW1MCSH004 -->
  <Businesses>
    <Business Name="Sikarbulls" Default="0" LegacyName="">
      <Resource Name="XYZ">
        <Uri UriType="PAGE" ResourceUri="http://ustfinder.com/ExternalApp.aspx/NewApp?CRWAAMONITOR" />
      </Resource>
      <Resource Name="ABC">
        <Uri UriType="PAGE" ResourceUri="http://ustpfuiv4.com/USTCMTUI/CMTMain.aspx?MenuItem=PartyMaintenance" />
      </Resource>
      <Resource Name="FGH">
        <Uri UriType="PAGE" ResourceUri="http://ustpfuiv4/USTCMTUI/CMTMain.aspx?MenuItem=RoleMaintenance" />
      </Resource>
      <Resource Name="CRWACTIONITEMS">
        <Uri UriType="PAGE" ResourceUri="http://ustfinder.com/Dashboard.aspx/ActionItem" />
      </Resource>
      <Resource Name="CRWAFS">
        <Uri UriType="PAGE" ResourceUri="http://ustpfuiv4/USTCMTUI/CMTMain.aspx?MenuItem=USTRelationships" />
      </Resource>

    </Business>
</Businesses>
karkou12Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rainer JeschorCommented:
Hi,
the following should work:
$var = Select-Xml -Path "C:\EE\EE_XMLAttr.xml" -XPath "//MLIFRTCUI/Businesses/Business/Resource[@Name='XYZ']/Uri" | Select-Object -ExpandProperty node | Select ResourceUri -Unique

Open in new window


HTH
Rainer
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
karkou12Author Commented:
Hi Rainer,

The code you posted worked well. Thank you Much.




I want to further enhance this to capture the data I need into a CSV file. The following code does it almost. For example in the below XML file in some <URI> tags the value of ResourceUri attribute have 2 urls and for some   <URI> tags the value of ResourceUri attribute have 1 url.

The problem I see with the code is it is able to capture 1 URL values correctly, but for tags having 2 or more URLS, data is not captured in a good way.
-----------------------------------------------------------------
   

$outfile = "C:\Temp\EIB Autaomation\out.csv"


$Profilervar = Select-Xml -Path "C:\Temp\EIB Autaomation\abc.xml" -XPath "//MLIFRTCUI/Businesses/Business/Resource" | % {$_.Node.Name} | select -unique

foreach( $value in $Profilervar)

    {
     

$var = Select-Xml -Path " C:\Temp\EIB Autaomation\abc.xml " -XPath "//MLIFRTCUI/Businesses/Business/Resource[@Name='$value']/Uri" | % {$_.Node.ResourceUri}

#$var

                		$OutputObj  = New-Object -Type PSObject
				$OutputObj | Add-Member -MemberType NoteProperty -Name ProfilerVariable -Value $value		

$OutputObj | Add-Member -MemberType NoteProperty -Name URL -Value $var
				
				$OutputObj | Export-Csv -Append -Path $outfile -Force -NoTypeInformation

}


The following is the CSV file
ProfilerVariable	URL
XYZ                       	A
ABC                         b
FGH	                        System.Object[]
JKL	                        System.Object[]

Open in new window



-------------------------------------------------------------------------------------------------------------------------------------------------
<MLIFRTCUI>
   <Businesses>
    <Business Name="Sikarbulls" Default="0" LegacyName="">
      <Resource Name="XYZ">
        <Uri UriType="PAGE" ResourceUri="http://ustfinder.com/ExternalApp.aspx/NewApp?CRWAAMONITOR" />
      </Resource>
      <Resource Name="ABC">
        <Uri UriType="PAGE" ResourceUri="http://ustpfuiv4.com/USTCMTUI/CMTMain.aspx?MenuItem=PartyMaintenance" />
      </Resource>
      <Resource Name="FGH">
        <Uri UriType="PAGE" ResourceUri="http://ustpfuiv4/USTCMTUI/CMTMain.aspx?MenuItem=RoleMaintenance" />
      </Resource>
      <Resource Name="JKL">
        <Uri UriType="PAGE" ResourceUri="http://ustfinder.com/Dashboard.aspx/ActionItem" />
    <Uri UriType="PAGE" ResourceUri="http://xasdefr.aspx" />
      </Resource>
      <Resource Name="CRWAFS">
        <Uri UriType="PAGE" ResourceUri="http://ustpfuiv4/USTCMTUI/CMTMain.aspx?MenuItem=USTRelationships" />
    <Uri UriType="PAGE" ResourceUri="http://abc.aspx" />
      </Resource>

    </Business>
</Businesses>
</MLIFRTCUI>
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.