[英]Parsing XML with PowerShell: Getting the value of an element with an attribute
我有一个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的日期值,而不是标签名称。
您正在尝试访问publish_date
XML元素的文本内容。 为此,请使用以下语法:
$xmlDoc.catalog.book | select author, title, publish_date, `
{$_.publish_date.'#text'}, {$_.publish_date.inprint}
我检查了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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.