简体   繁体   English

Powershell将xml值解析为csv

[英]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. 我有一个xml文件,其中包含我尝试在每一行中捕获的两个不同的值。 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. 我正在尝试使用Powershell将它们解析为csv,两列,其中一列是设备ID,一列是MAC地址,带有标题。 Any suggestions would be greatly appreciated. 任何建议将不胜感激。 Below is a sample of what the xml file looks like that I would be working with. 以下是我将使用的xml文件的示例。 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>

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

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... 有了这些信息,您应该能够取得进步。

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 = @"
<?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 : 这是$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. 如果需要,您应该能够将其写出到文件中。

You need to use .Net classes here, pure PS won't help. 您需要在此处使用.Net类,纯PS不会起作用。 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. 保存到文件中还有许多其他答复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM