I have a nested XML, that I need to convert to CSV using Powershell. Unfortunately, I am more at a beginner's level and was not able to solve this problem with the existing threads, I found online.
I tried it with reading the XML file into Powershell and creating a new object, but my export to csv doesn't even contain that unsufficient result... :(
The XML file I have looks like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<Data source="Jhonny" datetime="2019-04-23T10:07:50+02:00" timezone="Europe">
<dealerships>
<location name="Germany">
<series parameter="Sold Cars" unit="car">
<value datetime="2019-04-22T00:00:00+02:00" value="7.3"/>
<value datetime="2019-04-22T01:00:00+02:00" value="7.8"/>
<value datetime="2019-04-22T02:00:00+02:00" value="7.0"/>
<value datetime="2019-04-22T03:00:00+02:00" value="6.0"/>
</series>
<series parameter="Sold Cars" unit="Auto">
<value datetime="2019-04-22T00:00:00+02:00" value="4.0"/>
<value datetime="2019-04-22T01:00:00+02:00" value="4.0"/>
<value datetime="2019-04-22T02:00:00+02:00" value="4.0"/>
<value datetime="2019-04-22T03:00:00+02:00" value="4.0"/>
</series>
</location>
<location name="USA">
<series parameter="Sold Cars" unit="car">
<value datetime="2019-04-22T00:00:00+02:00" value="5.1"/>
<value datetime="2019-04-22T01:00:00+02:00" value="4.1"/>
<value datetime="2019-04-22T02:00:00+02:00" value="3.6"/>
<value datetime="2019-04-22T03:00:00+02:00" value="3.1"/>
</series>
<series parameter="Sold Cars" unit="Auto">
<value datetime="2019-04-22T00:00:00+02:00" value="3.0"/>
<value datetime="2019-04-22T01:00:00+02:00" value="3.0"/>
<value datetime="2019-04-22T02:00:00+02:00" value="3.0"/>
<value datetime="2019-04-22T03:00:00+02:00" value="3.0"/>
</series>
</location>
</dealerships>
</Data>
The result I am aiming for would look like this:
Location;Date/Time;Sold Cars car;Sold Cars Auto Germany; 2019-04-22T00:00:00+02:00; 7.3;4.0 Germany; 2019-04-22T00:00:00+02:00; 7.8;5.0 Germany; 2019-04-22T00:00:00+02:00; 7.0;3.0 Germany; 2019-04-22T00:00:00+02:00; 6.0;4.0 USA; 2019-04-22T00:00:00+02:00; 5.1;3.0 USA; 2019-04-22T00:00:00+02:00; 4.1;6.0 USA; 2019-04-22T00:00:00+02:00; 3.6;1.0 USA; 2019-04-22T00:00:00+02:00; 3.1;8.0
As I haven't really gotten anywhere, I don't think my code helps, but here is how I tried to solve it, but failed:
$xml = "C:\Users\[me]\Convert_XML_to_CSV\cars.xml"
$obj = New-Object System.XML.XMLDocument
$obj.Load("$xml")
foreach ($i in $_.Data.dealerships.location) {
$o = New-Object Object
Add-Member -InputObject $o -MemberType NoteProperty -Name location -Value $obj.Data.dealerships.Location $i $o
} | Export-Csv "result.csv" -Delimiter "," -NoType -Encoding UTF8