简体   繁体   中英

Parsing XML Files with PowerShell

To give an idea of how the XML code looks like that I am working with here.

<content>
<text id="new-users">
<Chinese>Nuevos usuarios</Chinese>
<English>New Users</English>
</text>
<text id="create-an-account">
<Chinese>Crear una Cuenta</Chinese>
<English>Create an Account</English>
</text>

I want to pull All of the English text between the < English > the test< /English > and have it listed then add a comma to the end. to export it to excel.

I can't understand what powershell is doing I am kind of lost. I can get "text" to print out a lot of times and I looked in to printing out the get-contents then piping the contents with a sort to only print the English and it didn't work.

So there are a couple of ways to get to the data. Here is the XPath way:

[xml]$x = Get-Content C:\Path\To\File.xml
$x.SelectNodes('//text/English') | Export-CSV C:\Path\To\Result.csv -NoTypeInfo

One way would be:

[xml] $xml = gc file.xml
$xml.content.text | select English | Export-CSV test.csv -notype

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM