Reputation: 19628
I have an XML file which has elements with attributes. I want to be able to read both the attribute value and the element value. While I can get the attribute value, when I try to read the element value it just returns the tag name, not the value.
XML file C:\Temp\books2.xml:
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date inprint="false">2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date inprint="true">2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date inprint="false">2000-11-17</publish_date>
<description>After the collapse of a nanotechnology
society in England, the young survivors lay the
foundation for a new society.</description>
</book>
</catalog>
What I've tried:
$xmlDoc = new-object -TypeName xml
$filePath = "C:\Temp\Books2.xml"
$xmlDoc.Load($filePath)
$xmlDoc.catalog.book | select author, title, publish_date, `
{$_.publish_date}, {$_.publish_date.inprint}
Result:
author : Gambardella, Matthew
title : XML Developer's Guide
publish_date : publish_date
$_.publish_date : publish_date
$_.publish_date.inprint : false
author : Ralls, Kim
title : Midnight Rain
publish_date : publish_date
$_.publish_date : publish_date
$_.publish_date.inprint : true
author : Corets, Eva
title : Maeve Ascendant
publish_date : publish_date
$_.publish_date : publish_date
$_.publish_date.inprint : false
How can I read the value of the element that also contains an attribute (in this case publish_date)? I want to read the date value for the publish_date, not the tag name.
Upvotes: 1
Views: 3702
Reputation: 19628
I checked the data type of the objects returned by PowerShell: ($xmlDoc.catalog).gettype().fullname
. It turns out the objects representing the nodes have data type System.Xml.XmlElement. So I tried using the XmlElement.InnerText property, and that worked:
$xmlDoc.catalog.book | select author, title, publish_date, `
{$_.publish_date.innertext}, {$_.publish_date.inprint} | format-list
Result:
author : Gambardella, Matthew
title : XML Developer's Guide
publish_date : publish_date
$_.publish_date.innertext : 2000-10-01
$_.publish_date.inprint : false
author : Ralls, Kim
title : Midnight Rain
publish_date : publish_date
$_.publish_date.innertext : 2000-12-16
$_.publish_date.inprint : true
author : Corets, Eva
title : Maeve Ascendant
publish_date : publish_date
$_.publish_date.innertext : 2000-11-17
$_.publish_date.inprint : false
Upvotes: 0
Reputation: 1009
You are trying to access the text content of the publish_date
XML element.
To do this, use the following syntax:
$xmlDoc.catalog.book | select author, title, publish_date, `
{$_.publish_date.'#text'}, {$_.publish_date.inprint}
Upvotes: 3