PowerShell:无法将值“ System.Object []”转换为类型“ System.Xml.XmlDocument”

问题描述 投票:0回答:2

[我遵循example,了解如何使用Powershell解析XML。我复制了示例XML文本的内容,并尝试使用给定的命令进行解析。一切正常。

然后我尝试解析自己的XML文件,但是在发出[xml]$bar = Get-Content test2.xml命令后,立即出现以下错误:

Cannot convert value "System.Object[]" to type "System.Xml.XmlDocument". Error: "The '=' character, hexadecimal value 0
x3D, cannot be included in a name. Line 5, position 19."
At line:1 char:1
+ [xml]$bar = Get-Content test2.xml
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : MetadataError: (:) [], ArgumentTransformationMetadataException
    + FullyQualifiedErrorId : RuntimeException

因此,我认为XML文件的内容不同仅会影响Powershell解析XML数据的能力?

我的XML文件内容:

<?xml version="1.0" encoding="UTF-8"?>

<metamarket id="lowvol">
    <market id="asx" marketDir="creditcentraligaapaclve_asx_m" mandatoryNews="False">
        <houseCode="CREDITBAAPACLVE" metahouseCode="CREDITBAAPACLVE"/>
    </market>

    <market id="bru" marketDir="creditcentraligaapaclve_bru_m" mandatoryNews="False">
        <houseCode="CREDITBAAPACLVE" metahouseCode="CREDITBAAPACLVE"/>
    </market>

    <market id="sto" marketDir="creditcentraligaapaclve_sto_m" mandatoryNews="False">
        <houseCode="CREDITBAAPACLVE" metahouseCode="CREDITBAAPACLVE"/>
    </market>
</metamarket>
xml powershell
2个回答
1
投票

错误消息指出了问题的根源,尽管它没有明确指出问题是element名称(加了强调):

错误:“ '='字符,十六进制值0x3D,不能包含在name中。第5行,位置19。“

XML中的元素名称(通常为节点名称)本身不能包含=字符。

相反,诸如=之类的标记中的id="lowvol"是属性名称与其值之间的分隔符

您的XML错误地使用了这样的标记-houseCode="CREDITBAAPACLVE"-直接在开始标记的<之后],因此houseCode="CREDITBAAPACLVE"被解释为元素名称

解决方案是给您的元素起一个适当的名称,例如house

<house houseCode="CREDITBAAPACLVE" metahouseCode="CREDITBAAPACLVE"/>

0
投票

我的XML文档格式错误,如@ mklement0所示

© www.soinside.com 2019 - 2024. All rights reserved.