Read large XML file with Powershell

I have a 500mb+ xml file I need to break apart, but every time I do Get-Content file it is too large for memory. Is there any way to process each node in the root one at a time similar to reading a stream?

The error: Exception of type 'System.OutOfMemoryException' was thrown.
LVL 8
RyanAndresAsked:
Who is Participating?
 
Chris DentConnect With a Mentor PowerShell DeveloperCommented:

You should be able to initialise an instance of System.Xml.XmlReader using the StreamReader.

e.g.

$Reader = New-Object IO.StreamReader("FullPathToFile")
$XmlReader = [Xml.XmlReader]::Create($Reader)

Since it's reading it as a stream rather than loading the entire file into memory it "should" work better. However, I cannot test that theory since I don't have any large XML files.

Chris
0
 
rockiroadsCommented:
0
 
RyanAndresAuthor Commented:
No, it uses $xml = [xml](gc file) ....

This is what I tried and it will load the entire file into memory.. which will cause the exception.
0
 
RyanAndresAuthor Commented:
Cool I used XmlTextReader and Writer.
0
All Courses

From novice to tech pro — start learning today.