【问题标题】:PowerShell: Cannot convert value "System.Object[]" to type "System.Xml.XmlDocument"PowerShell:无法将值“System.Object[]”转换为类型“System.Xml.XmlDocument”
【发布时间】:2020-02-22 02:17:40
【问题描述】:

我正在关注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


    【解决方案1】:

    错误消息指出了问题的根源,尽管它没有明确指出 元素 名称是问题所在(添加了重点):

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

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

    相比之下,id="lowvol" 等标记中的= 是属性名称与其值之间的分隔符。

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

    解决办法是给你的元素一个合适的名字,比如house:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多