使用Powershell转换XML直到JSON

时间:2017-11-17 12:35:07

标签: json xml powershell

这感觉就像一个n00b问题,但我无法弄清楚如何解决它。

我在文件中有一些XML:

<EquipmentMessage>
    <Equipment>
        <UnitNo>AT_456578</UnitNo>
        <UnitRegNo>456578</UnitRegNo>
        <UnitRegNoCountry>AT</UnitRegNoCountry>
        <UnitOwner>000692</UnitOwner>
        <UnitOwnerID>6406601449</UnitOwnerID>
        <UnitOwnerTerms/>
        <UnitAdministrator>HAP</UnitAdministrator>
        <UnitAdministratorTerms/>
        <UnitManagement/>
        <UnitManagementTerms/>
        <UnitPoolID/>
        <MainCategory>TRUCK</MainCategory>
        <AssetType>HEAVYHigh</AssetType>
        <UnitCategory>HEAVY</UnitCategory>
        <UnitSize>High</UnitSize>
        <UnitType/>
        <UnitSubType>18 t</UnitSubType>
        <UnitRedeliverAct/>
        <SubContractorID>6406601449</SubContractorID>
    </Equipment>
</EquipmentMessage>

我想将其转换为JSON并保存在另一个文件中。所以我读了XML文件并使用:

转换为JSON
[xml]$xmlfile = Get-Content .\Equipment.xml
$json = Convertto-json $xmlfile

但是JSON对象包含:

[
    [
        [
            "System.Xml.XmlElement",
            "System.Xml.XmlElement",
            "System.Xml.XmlElement",
            "System.Xml.XmlElement",
            "System.Xml.XmlElement",
            "System.Xml.XmlElement",
            "System.Xml.XmlElement",
            "System.Xml.XmlElement",
            "System.Xml.XmlElement",
            "System.Xml.XmlElement",
            "System.Xml.XmlElement",
            "System.Xml.XmlElement",
            "System.Xml.XmlElement",
            "System.Xml.XmlElement",
            "System.Xml.XmlElement",
            "System.Xml.XmlElement",
            "System.Xml.XmlElement",
            "System.Xml.XmlElement",
            "System.Xml.XmlElement"
        ]
    ]
]

我在阅读XML时是错误的还是我在转换中遗漏了某些东西,还是其他东西?

0 个答案:

没有答案