7

I have an XML of couple of gigabytes. There are no spaces in the XML.

So I wrote a little C# code to split in single files (which has some additional code to perform some stuff e.g. randomizing while testing)

using (XmlReader MyReader = XmlReader.Create(@"d:\xml\test.xml"))
            {
                while (MyReader.Read())
                {
                    switch (MyReader.NodeType)
                    {
                        case XmlNodeType.Element:
                            if (MyReader.Name == "Customer")
                            {
                                XElement el = XElement.ReadFrom(MyReader) as XElement;
                                if (el != null)
                                {
                                    custNumber = (string)el.Element("CustNumber");
                                    output = @"d:\xml\output\" + custNumber;

                                    File.WriteAllText(output, el.ToString());
                                }                                    
                            }
                            break;
                    }
                }
            }

I then parse the resulting files with PowerShell, basically because I find it easier to work with on the server while specs can change and I can on the fly change the script.

So... what is the easiest way to convert the above to PowerShell also, putting [.Net here] before everything ? would I have to read byte for byte just in the case it has "<cust" on one line and "omer>" on the next?

1 Answer 1

12

This should be pretty close to what you wanted to do in Powershell:

$f = [System.Xml.XmlReader]::create("d:\xml\test.xml")

while ($f.read())
{
    switch ($f.NodeType)
    {
        ([System.Xml.XmlNodeType]::Element) # Make sure to put this between brackets
        {
            if ($f.Name -eq "Customer")
            {
                $e = [System.Xml.Linq.XElement]::ReadFrom($f)

                if ($e -ne $null)
                {
                    $custNumber = [string] $e.Element("CustNumber")

                    $e.ToString() | Out-File -Append -FilePath ("d:\xml\output\"+$e.ToString())
                }
            }
            break
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Had to move the switch to a if ($f.NodeType -eq [Syste,,,) and interestingly in PowerShell had to use .Value of the element contrary to C#.
@edelwater: adding brackets around the switch option fixed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.