我在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/