Dennis Hayden
Dennis Hayden

Reputation: 184

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.

Upvotes: 0

Views: 3481

Answers (2)

manojlds
manojlds

Reputation: 301037

One way would be:

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

Upvotes: 1

EBGreen
EBGreen

Reputation: 37720

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

Upvotes: 4

Related Questions