使用 Powershell 将 xml 转换为 csv 时出现问题

Trouble with converting a xml into a csv with Powershell

所以我有一个非常规的 xml,我想将其转换为 csv,我已经到了不知道去哪里进一步查看的地步。

<?xml version="1.0" encoding="UTF-16"?>
-<Records count="88">

-<Record parentId="0" moduleId="618" levelGuid="bf1e60be-cae0-428d-88eb-60cce2ca7e80" levelId="416" contentId="889443">
<Field type="1" guid="55ef2cec-bcff-4bff-ad83-43f55205fad2" id="24035">Exception Requests >> Exception Requests</Field>
<Field type="1" guid="de997cd6-e53c-4ff1-81e9-17a9f98b3043" id="24036">Name</Field>
<Field type="1" guid="d4d1d2ad-c4af-454a-afad-851617a94874" id="24037">Additional Access</Field>
<Field type="1" guid="0f464953-42d1-4487-96b1-6b3d1ba85558" id="24038">Accès supplémentaire</Field>
<Field type="1" guid="91cf5fd2-70dc-4e37-bca9-046851e2c763" id="24039">cada8def-650f-443d-9ef3-53b74ecc16b2</Field>
<Field type="1" guid="5a5bdeb2-8f98-4f56-8c77-e1294b4e1cf1" id="24040">476</Field>
<Field type="1" guid="2a67dc86-c01d-4927-8a08-7557543248d2" id="24041">1</Field>
<Field type="1" guid="395a0e50-52cd-4d61-8653-94ad41f5d7da" id="24042">b809048d0be00ec524a7c7c95c1a1f8a</Field>
</Record>

-<Record parentId="0" moduleId="618" levelGuid="bf1e60be-cae0-428d-88eb-60cce2ca7e80" levelId="416" contentId="889317">
<Field type="1" guid="55ef2cec-bcff-4bff-ad83-43f55205fad2" id="24035">Exception Request Extensions >> Exception Request Extensions</Field>
<Field type="1" guid="de997cd6-e53c-4ff1-81e9-17a9f98b3043" id="24036">Name</Field>
<Field type="1" guid="d4d1d2ad-c4af-454a-afad-851617a94874" id="24037">Most Recent Approved Expiration Date Helper</Field>
<Field type="1" guid="0f464953-42d1-4487-96b1-6b3d1ba85558" id="24038">Aide sur la date max.</Field>
<Field type="1" guid="91cf5fd2-70dc-4e37-bca9-046851e2c763" id="24039">8b3fbad5-2806-4a64-8e2b-fe5dfaa4562d</Field>
<Field type="1" guid="5a5bdeb2-8f98-4f56-8c77-e1294b4e1cf1" id="24040">8098</Field>
<Field type="1" guid="2a67dc86-c01d-4927-8a08-7557543248d2" id="24041">1</Field>
<Field type="1" guid="395a0e50-52cd-4d61-8653-94ad41f5d7da" id="24042">594a6240a3a606cf9720189a5f15980c</Field>
</Record>
</Records>

我要的是把每条记录的字段都做成列,id可以做name,后面的text做value。 (最后一张)

目前我拥有的是:

#read from file
"reading..."
[xml]$inputFile = Get-Content "Exportation_Field.xml"
"exporting..."
#export xml as csv
if ($inputFile.Records.childnodes )
    {
    $inputFile.Records.record.childnodes | Select-Object "#text" | Export-Csv "exportCSV.csv" -NoTypeInformation -Delimiter:";" -Encoding:UTF8 
    "complete"
    }

这returns所有字段的所有#text一个接一个地在一个列中。

这就是我想要得到的:outputCSV

我觉得某种循环可能会有所帮助,但作为 PowerShell 的新手,我不知道如何在这种情况下使用它们。

这是预期的结果:

expectedCSV

提前致谢。

为了获得您想要的结果,您需要做的是制作每个 ID 为 属性 的对象。我们可以使用许多工具,在本例中我将使用 Select-XML

select-xml -Path "Exportation_Field.xml" -XPath "//Record" |
    ForEach-Object {
        $_.node.field | ForEach-Object -Begin {$hash = [ordered]@{}} -Process {
            $hash.add($_.id,$_.'#text')
        } -End {[PSCustomObject]$hash}
}

在这个例子中,你最终会得到 2 个对象

24035 : Exception Requests >> Exception Requests
24036 : Name
24037 : Additional Access
24038 : Accès supplémentaire
24039 : cada8def-650f-443d-9ef3-53b74ecc16b2
24040 : 476
24041 : 1
24042 : b809048d0be00ec524a7c7c95c1a1f8a

24035 : Exception Request Extensions >> Exception Request Extensions
24036 : Name
24037 : Most Recent Approved Expiration Date Helper
24038 : Aide sur la date max.
24039 : 8b3fbad5-2806-4a64-8e2b-fe5dfaa4562d
24040 : 8098
24041 : 1
24042 : 594a6240a3a606cf9720189a5f15980c

只需在末尾添加Export-Csv。我选择使用 Foreach-Object cmdlet 的 -Begin-End 参数,也可以这样写。

select-xml -Path "Exportation_Field.xml" -XPath "//Record" |
    ForEach-Object {
        $hash = [ordered]@{}
        $_.node.field | ForEach-Object {
            $hash.add($_.id,$_.'#text')
        }
        [PSCustomObject]$hash
} | Export-Csv -Path OutFile.csv -NoTypeInformation