我是PowerShell的新手,但有很强的C#背景。
我要完成的任务是下载一些XML,然后将XML文件中的一些元素保存到一个.csv文件中。我不需要XML中的所有元素,只需要几个项目。
XML看起来像这样:
<result>
<case>
<InputData>
<Id>1</Id>
<Firstname>John</Firstname>
<Lastname>Smith</Lastname>
<....lots more properties and subnodes I don't need>
</InputData>
</case>
</result>我已经设法使用如下代码下载了XML:
$downloaded = ([Xml](New-Object Net.WebClient).DownloadString("http://url.to.server/file.xml"))现在,我需要从XML中提取一些属性,并将其导出到CSV文件。
我知道我可以用$downloaded.result.case.InputData.Firstname获得数据,但需要一些关于如何提取一些属性并另存为CSV的建议。
发布于 2009-06-30 16:46:05
你可以这样做:
$downloaded.result.case | %{ $_.InputData }`
| select Firstname,Lastname `
| Export-Csv -path foo.csv -NoTypeInformation发布于 2009-06-30 13:33:57
XSLT是一种(?)当涉及到将XML转换为其他内容时的标准方法。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="InputData">
<xsl:value-of select="concat('"', Id, '"')" />
<xsl:value-of select="','" />
<xsl:value-of select="concat('"', Firstname, '"')" />
<xsl:value-of select="','" />
<xsl:value-of select="concat('"', Lastname, '"')" />
<xsl:value-of select=" " />
</xsl:template>
</xsl:stylesheet>一些Powershell上下文:www.hanselman.com: XSLT with Powershell
https://stackoverflow.com/questions/1063333
复制相似问题