LilithGoddess
LilithGoddess

Reputation: 165

Powershell parse xml values into csv

I have an xml file that contains two different values that I am trying to capture in each line. I am trying to use Powershell to parse them into a csv, two columns, one being Device ID and one being MAC address, with headings. Any suggestions would be greatly appreciated. Below is a sample of what the xml file looks like that I would be working with. Thank you for your time.

<?xml version="1.0" encoding="utf-8"?>
<DeviceConfig>
    <Device RegisterID="1021" MacID="A4F1E85D7D86" />
    <Device RegisterID="1022" MacID="A4F1E85D056E" />
    <Device RegisterID="1023" MacID="A4F1E85CAAEE" />
    <Device RegisterID="1024" MacID="A4F1E85DB284" />
    <Device RegisterID="1025" MacID="A4F1E85D7021" />
    <Device RegisterID="1026" MacID="A4F1E85D034A" />
    <Device RegisterID="1027" MacID="A4F1E85CAD59" />
    <Device RegisterID="1028" MacID="A4F1E85DAFA2" />
    <Device RegisterID="1029" MacID="A4F1E85D050E" />
    <Device RegisterID="1030" MacID="A4F1E85DAF89" />
    <Device RegisterID="1031" MacID="A4F1E85DA80E" />
</DeviceConfig>

Upvotes: 2

Views: 3244

Answers (3)

Kory Gill
Kory Gill

Reputation: 7163

You can replace my here string with a call to Get-Content for your actual file, but this works for me, and does not need to bring in .NET classes.

Code

$xml = @"
<?xml version="1.0" encoding="utf-8"?>
<DeviceConfig>
    <Device RegisterID="1021" MacID="A4F1E85D7D86" />
    <Device RegisterID="1022" MacID="A4F1E85D056E" />
    <Device RegisterID="1023" MacID="A4F1E85CAAEE" />
    <Device RegisterID="1024" MacID="A4F1E85DB284" />
    <Device RegisterID="1025" MacID="A4F1E85D7021" />
    <Device RegisterID="1026" MacID="A4F1E85D034A" />
    <Device RegisterID="1027" MacID="A4F1E85CAD59" />
    <Device RegisterID="1028" MacID="A4F1E85DAFA2" />
    <Device RegisterID="1029" MacID="A4F1E85D050E" />
    <Device RegisterID="1030" MacID="A4F1E85DAF89" />
    <Device RegisterID="1031" MacID="A4F1E85DA80E" />
</DeviceConfig>
"@

$xmlData = [xml]$xml

$xmlData.DeviceConfig.ChildNodes | ConvertTo-Csv -NoTypeInformation

Output

"RegisterID","MacID"
"1021","A4F1E85D7D86"
"1022","A4F1E85D056E"
"1023","A4F1E85CAAEE"
"1024","A4F1E85DB284"
"1025","A4F1E85D7021"
"1026","A4F1E85D034A"
"1027","A4F1E85CAD59"
"1028","A4F1E85DAFA2"
"1029","A4F1E85D050E"
"1030","A4F1E85DAF89"
"1031","A4F1E85DA80E"

With this information, you should be able to make progress...

Upvotes: 4

Jason Boyd
Jason Boyd

Reputation: 7029

I think something like this will work. Here I am reading the XML from a string, you may be importing it from a file:

$xml = @"
<?xml version="1.0" encoding="utf-8"?>
<DeviceConfig>
    <Device RegisterID="1021" MacID="A4F1E85D7D86" />
    <Device RegisterID="1022" MacID="A4F1E85D056E" />
    <Device RegisterID="1023" MacID="A4F1E85CAAEE" />
    <Device RegisterID="1024" MacID="A4F1E85DB284" />
    <Device RegisterID="1025" MacID="A4F1E85D7021" />
    <Device RegisterID="1026" MacID="A4F1E85D034A" />
    <Device RegisterID="1027" MacID="A4F1E85CAD59" />
    <Device RegisterID="1028" MacID="A4F1E85DAFA2" />
    <Device RegisterID="1029" MacID="A4F1E85D050E" />
    <Device RegisterID="1030" MacID="A4F1E85DAF89" />
    <Device RegisterID="1031" MacID="A4F1E85DA80E" />
</DeviceConfig>
"@

$csv = `
    Select-Xml -Content $xml -XPath "//Device" `
    | Select -ExpandProperty Node `
    | ConvertTo-Csv -NoTypeInformation

$csv

This is the content of $csv:

"RegisterID","MacID"
"1021","A4F1E85D7D86"
"1022","A4F1E85D056E"
"1023","A4F1E85CAAEE"
"1024","A4F1E85DB284"
"1025","A4F1E85D7021"
"1026","A4F1E85D034A"
"1027","A4F1E85CAD59"
"1028","A4F1E85DAFA2"
"1029","A4F1E85D050E"
"1030","A4F1E85DAF89"
"1031","A4F1E85DA80E"

You should be able to write that out to a file if you need.

Upvotes: 2

Mike Makarov
Mike Makarov

Reputation: 1356

You need to use .Net classes here, pure PS won't help. Fortunately, it's easy.

Add-Type -AssemblyName System.Xml.Linq
$xe = [System.Xml.Linq.XElement]::Parse($xml)
$xe.Elements() | % {$_.Attribute('RegisterID').Value + ", " + $_.Attribute('MacID').Value}

This prints what you need to output. Saving to file is covered by many other replies.

Upvotes: 0

Related Questions