powershell - 在Powershell中转义冒号

标签 powershell escaping colon

我在powershell中有以下代码,试图读回一段xml。

$path = "C:\path\8429006775491.xml"
$xml = [xml](Get-Content $path)
$xml.ern:NewReleaseMessage

问题是冒号(:),我试图用'进行转义,但它似乎不起作用。还尝试将其放入{}
如果我在xml文件本身中编辑冒号并相应地更改代码,则它读得很好,但是不幸的是,这不是一个选择。任何帮助都非常感谢。

最佳答案

为了让.NET了解 namespace 前缀,您需要一个 namespace 管理器对象:

$path = "C:\path\8429006775491.xml"
$xml = [xml](Get-Content $path)

$XmlNSManager = New-Object System.Xml.XmlNamespaceManager -ArgumentList $xml.NameTable
$XmlNSManager.AddNamespace('ern','http://ddex.net/xml/ern/341')
$XmlNSManager.AddNamespace('xsi','http://www.w3.org/2001/XMLSchema-instance')

# Now you can use SelectNodes() with your namespace manager:

# If NewReleaseMessage is the root node
$xml.SelectNodes('/ern:NewReleaseMessage',$XmlNSManager)

# If NewReleaseMessage is a descendant of the root node
$xml.SelectNodes('//ern:NewReleaseMessage',$XmlNSManager)

关于powershell - 在Powershell中转义冒号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31724250/

相关文章:

php - 转义变量

javascript - 在 WordPress 中转义 PHP 函数内部的 JavaScript 代码片段

batch-file - Windows 批处理语法 - 变量名称中冒号的含义

powershell - 如何在PowerShell中对动态变量的内容进行转义?

powershell - 从 SSIS 调用 Powershell 脚本时无法检索 stderror 和 stdout

prolog - gprolog 中的 ANSI 转义字符

javascript冒号运算符混淆

linux - 从文件名中删除冒号

powershell - 使用 WindowServerCore 在 Docker 中没有 PowerShell 帮助

vba - 如何从PowerShell将丢失的可选参数传递给VBA方法