powershell解析xml转义等号

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

我有一个 xml 文件,我尝试使用此 cmd 在 powershell 中解析它:

[xml]$userfile = Get-Content $userfilepath

失败是因为 xml 文件的一个字段包含等号,如下所示:

<photo>https://url/?include_plugin=downloader&file=/photos/33.jpg</photo>

对于照片字段,我需要提取 url,而 url 恰好包含等号 (=)。

错误是: 无法将值“System.Object[]”转换为类型“System.Xml.XmlDocument”。错误:“'=' 是意外标记。预期标记是 ';'。”

我在另一个系统中生成xml文件,我尝试通过添加“”来更改:

<photo>\"https://url/?include_plugin=downloader&file=/photos/33.jpg\"</photo>

我也有同样的错误。

有什么想法吗?

谢谢。

powershell xml-parsing
1个回答
0
投票

问题不是

=
,而是
&...
是 XML 中的转义序列,因此解析器看到
&file=
并显示“我无法识别任何包含
=
的转义序列”。

适当地转义

&
(如
&amp;
),它会起作用:

<photo>https://url/?include_plugin=downloader&amp;file=/photos/33.jpg</photo>
© www.soinside.com 2019 - 2024. All rights reserved.