我有一个XML文件,其中包含具有属性的元素。我希望能够读取属性值和元素值。虽然我可以获取属性值,但当我尝试读取元素值时,它只返回标记名称,而不是值。
XML文件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>
我尝试了什么:
$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}
结果:
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
如何读取同时包含属性的元素的值(在本例中为publish_date)?我想读取publish_date的日期值,而不是标签名称。
答案 0 :(得分:2)
您正在尝试访问publish_date
XML元素的文本内容。
为此,请使用以下语法:
$xmlDoc.catalog.book | select author, title, publish_date, `
{$_.publish_date.'#text'}, {$_.publish_date.inprint}
答案 1 :(得分:0)
我检查了PowerShell返回的对象的数据类型:($xmlDoc.catalog).gettype().fullname
。事实证明,表示节点的对象具有数据类型System.Xml.XmlElement。所以我尝试使用XmlElement.InnerText属性,这有用:
$xmlDoc.catalog.book | select author, title, publish_date, `
{$_.publish_date.innertext}, {$_.publish_date.inprint} | format-list
结果:
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