Link to home
Start Free TrialLog in
Avatar of karkou12
karkou12

asked on

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>
ASKER CERTIFIED SOLUTION
Avatar of Rainer Jeschor
Rainer Jeschor
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of karkou12
karkou12

ASKER

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>