在大小写中冒出一个冒号 [英] Escaping a colon in powershell

查看:290
本文介绍了在大小写中冒出一个冒号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

问题是冒号(:),我试图逃脱它与'但它似乎不起作用。还试着把它放在{}
如果我编辑xml文件本身的冒号,并相应地更改代码,它可以读回来,但不幸的是,这不是一个选项。

解决方案

为了让.NET了解命名空间前缀,您需要一个Namespace Manager对象:

  $ 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')

#现在您可以使用SelectNodes()与您的命名空间管理器:

#如果NewReleaseMessage是根节点
$ xml.SelectNodes('/ ern:NewReleaseMessage',$ XmlNSManager)

#如果NewReleaseMessage是根节点的后代
$ xml.SelectNodes('// ern:NewReleaseMessage',$ XmlNSManager)


I have the following piece of code in powershell trying to read back a piece of xml.

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

The problem is the colon(:), i've tried to escape it with ' but it doesn't seem to work. Also tried to put it in {} If i edit the colon in the xml file itself and change the code accordingly it reads back fine but unfortunately that is not an option. Any help really appreciated.

解决方案

In order for .NET to understand the namespace prefixes, you'll need a Namespace Manager object:

$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)

这篇关于在大小写中冒出一个冒号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆