Powershell将xml值解析为csv

时间:2016-09-23 23:17:56

标签: xml powershell csv

我有一个xml文件,其中包含我尝试在每一行中捕获的两个不同的值。我正在尝试使用Powershell将它们解析为csv,两列,一列是设备ID,另一列是MAC地址,带有标题。任何建议将不胜感激。下面是我将使用的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>

3 个答案:

答案 0 :(得分:3)

您可以使用对实际文件的Get-Content调用来替换我的here字符串,但这对我有用,并且不需要引入.NET类。

<强>代码

$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

<强>输出

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

有了这些信息,您应该能够取得进展......

答案 1 :(得分:2)

我觉得这样的事情会起作用。这里我从字符串中读取XML,您可以从文件中导入它:

$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

这是$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"

如果需要,您应该可以将其写入文件。

答案 2 :(得分:-1)

你需要在这里使用.Net类,纯PS无济于事。幸运的是,这很容易。

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

这将打印您需要输出的内容。许多其他回复都涵盖了保存到文件。