I have to parse the file below and display a few tags in a report.
<CommChannelQueryResponse>
<CommChannel>
<ChannelName>C1</ChannelName>
<AdapterAttribute>
<Name>Filename</Name>
<Value>a.txt</Value>
</AdapterAttribute>
<AdapterAttribute>
<Name>Directory</Name>
<Value>/dir1</Value>
</AdapterAttribute>
<AdapterAttribute>
<Name>ArchiveDirectory</Name>
<Value>/archive1</Value>
</AdapterAttribute>
...
</CommChannel>
<CommChannel>
...
</CommChannelQueryResponse>
Based on another post in this forum, I tried the following and it works.
[xml]$XmlDocument = Get-Content commchannels.xml
$XmlDocument.CommChannelQueryResponse.CommChannel |
Format-Table -Property @{L="Name"; E={$_.ChannelName}},
@{L="Filename";Expression={$_.AdapterAttribute.Value[0]}}
Name Filename ---- -------- C1 a.txt C2 b.txt
I am trying to improve on this since this code depends on the "Filename" being the first attribute. How do I change this to display the AdapterAttribute.Value
which has AdapterAttribute.Name = "Filename"
?
I tried the following and as you can see it does not display anything for "Filename".
$XmlDocument.CommChannelQueryResponse.CommChannel |
Format-Table -Property @{L="Name";E={$_.ChannelName}},
@{L="Filename";Expression={
$_.AdapterAttribute.Value | Where-Object $_.AdapterAttribute.Name = "Filename"
}}
Name Filename ---- -------- C1 C2
Don't know if you want to get into Xpath expressions... (case sensitive)
(select-xml "/CommChannelQueryResponse/CommChannel/AdapterAttribute" file.xml).node
Name Value
---- -----
Filename a.txt
Directory /dir1
ArchiveDirectory /archive1
(select-xml "/CommChannelQueryResponse/CommChannel/AdapterAttribute[Name/text()='Filename']" file.xml).node
Name Value
---- -----
Filename a.txt
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.