我有一个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>
最佳答案
您可以使用对您的实际文件的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"
有了这些信息,您应该能够取得进步。