Another little problem parsing XML file in Powershell. I'm working on a big XML file that seems to be quite like this one that I found online and a bit modified:
<?xml version="1.0"?>
<ROOT>
<Customers>
<Customer Region="USA" CustomerName="Arshad Ali" CustomerID="C001">
<Orders>
<Order OrderDate="2012-07-04T00:00:00" OrderID="10248">
<OrderDetail Quantity="5" ProductID="10"/>
<OrderDetail Quantity="12" ProductID="11"/>
<OrderDetail Quantity="10" ProductID="42"/>
</Order>
</Orders>
<Address> Address line 1, 2, 3</Address>
</Customer>
<Customer Region="USA" CustomerName="Paul Henriot" CustomerID="C002">
<Orders>
<Order OrderDate="2011-07-04T00:00:00" OrderID="10245">
<OrderDetail Quantity="12" ProductID="11"/>
<OrderDetail Quantity="10" ProductID="42"/>
</Order>
</Orders>
<Address> Address line 5, 6, 7</Address>
</Customer>
<Customer Region="USA" CustomerName="Carlos Gonzlez" CustomerID="C003">
<Orders>
<Order OrderDate="2012-08-16T00:00:00" OrderID="10283">
<OrderDetail Quantity="3" ProductID="72"/>
</Order>
</Orders>
<Address> Address line 1, 4, 5</Address>
</Customer>
</Customers>
</ROOT>
I need to reach and retrive the Quantity field in row number 7 with dot notation. I'm using this code
PS C:\Users> [xml]$xmlElm = Get-Content -Path "C:\Users\Admin\Downloads\Test1.xml"
PS C:\Users> $xmlElm.ROOT.Customers.Customer
Country : USA
CustomerName : Arshad Ali
CustomerID : C001
Orders : Orders
Address : Address line 1, 2, 3
Country : USA
CustomerName : Paul Henriot
CustomerID : C002
Orders : Orders
Address : Address line 5, 6, 7
Country : USA
CustomerName : Carlos Gonzlez
CustomerID : C003
Orders : Orders
Address : Address line 1, 4, 5
...and now? Everything I've tried returns error or blank, I don't understand how to reach the lower fields values; can anybody help me?
You simply have to tell it which node you are interested in, for example giving it the index (0 based) of the node, in square brackets.
[xml]$xmlElm = Get-Content -Path "C:\Users\ColinD\Downloads\Test.xml"
$xmlElm.ROOT.Customers.Customer[0].Orders.Order.OrderDetail[0].Quantity
This will output 5