I have a large xml response from querying an api ~2,000,000 lines and I am unsure how to parse this in a timely manner. I only need to get the value of 2 elements for every node as I go through the file and save it to a hashtable. It has been an hour since running the code below and the script is not done parsing. Any ideas?
UPDATE: I found this question here on Stackoverflow How can i use XmlReader in PowerShell to stream big/huge XML files? It parses really fast.
Xml file excerpt
<Response>
<Node_List>
<Node>
<Element 1></Element 1>
<Element 2></Element 2>
<Element 3></Element 3>
<Element 4></Element 4>
<Element 5></Element 5>
<Element 6></Element 6>
<Element 7></Element 7>
<Element 8></Element 8>
</Node>
<Node>
</Node>
</Node_List>
</Response>
Powershell Code
$hashTable = $null
$hashTable = @{}
[xml]$response = API query here
$elementCount =
$response.SelectNodes("path/to/get/element/count").Count
for($i=0; $i -lt $elementCount; $i++)
{
[string] $elementOne = $response.SelectNodes("/path/to/first/element")[$i].InnerText
[string] $elementOne = $response.SelectNodes("/path/to/second/element")[$i].InnerText
try
{
$hashtable.Add($elementOne,$elementTwo)
}
catch
{
# This is to catch the exceptions that come up when duplicate values are added to the
hashtable
}