Xml Powershell新对象:未找到构造函数
我正试图创建一个函数来获取de NameSpaceManager,但我收到一条消息错误,我不明白为什么Xml Powershell新对象:未找到构造函数,xml,powershell,Xml,Powershell,我正试图创建一个函数来获取de NameSpaceManager,但我收到一条消息错误,我不明白为什么 function Get-XmlNamespaceManager([ xml ]$xml, [string]$NamespaceURI = "") { if ([string]::IsNullOrEmpty($NamespaceURI)) { $NamespaceURI = $xml.DocumentElement.NamespaceURI } [System.X
function Get-XmlNamespaceManager([ xml ]$xml, [string]$NamespaceURI = "")
{
if ([string]::IsNullOrEmpty($NamespaceURI)) { $NamespaceURI = $xml.DocumentElement.NamespaceURI }
[System.Xml.XmlNamespaceManager]$xmlNsManager = New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
$xmlNsManager.AddNamespace("ns", $NamespaceURI)
return ,$xmlNsManager
}
信息如下:
New-Object : A constructor was not found. Cannot find an appropriate constructor for type System.Xml.XmlNamespaceManager.
At C:\Scripts\Add Archive Handle\Add_Archive_Handle.ps1:35 char:53
+ ... NsManager = New-Object System.Xml.XmlNamespaceManager($xml.NameTable) ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [New-Object], PSArgumentException
+ FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand
但我的$xmlNsManager返回:
PS C:\Path\To\Myfiles> $xmlNsManager
xmlns
xml
ns
谢谢你的帮助
这是我代码的一部分:
Get-ChildItem -Path 'C:\Scripts\Add Archive Handle\Source\' -Recurse -Include "*.imdi" -File | ForEach-Object {
$NodePath = $xml.METATRANSCRIPT.Session.Resources.MediaFile.ResourceLink,$xml.METATRANSCRIPT.Session.Resources.WrittenResource.ResourceLink
[xml]$xml = Get-Content $_.FullName;
$xml= $xml.METATRANSCRIPT.OuterXml;
$xmlAtt = $xml.CreateAttribute("ArchiveHandle")
$dt = $xml.METATRANSCRIPT.Attributes.GetNamedItem('Date')
$xmlAtt = $xml.METATRANSCRIPT.Attributes.InsertBefore($xmlAtt, $dt);
function Get-XmlNamespaceManager([ xml ]$xml, [string]$NamespaceURI =""){
if ([string]::IsNullOrEmpty($NamespaceURI)) { $NamespaceURI = $xml.DocumentElement.NamespaceURI }
[System.Xml.XmlNamespaceManager]$xmlNsManager =New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
$xmlNsManager.AddNamespace("ns", $NamespaceURI)
return ,$xmlNsManager }
function Get-FullyQualifiedXmlNodePath([string]$NodePath, [string]$NodeSeparatorCharacter = '.'){
return "/ns:$($NodePath.Replace($($NodeSeparatorCharacter), '/ns:'))"}
function Get-XmlNode([ xml ]$xml, [string]$NodePath, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.'){
$xmlNsManager = Get-XmlNamespaceManager -XmlDocument $xml -NamespaceURI $NamespaceURI
[string]$fullyQualifiedNodePath = Get-FullyQualifiedXmlNodePath -NodePath $NodePath -NodeSeparatorCharacter $NodeSeparatorCharacter
$node = $xml.SelectSingleNode($fullyQualifiedNodePath, $xmlNsManager)
return $node}}
这是特定错误的位置。这并没有特别的错误,但它只是因为您如何调用函数而成为错误
$xmlNsManager
正如您可能知道的(因为我假设您创建了这个函数),该函数接受两个参数,($xml
和$namespaceuri
)$namespaceuri
可以是null
并且不是强制性的,但是尽管您没有指定$xml
是强制性的,但这是因为您必须使用它。(
)
如果我运行该函数,但将$xml
参数如下所示:
Get-XmlNamespaceManager -xml @"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Identity PUBLIC "point.dtd" "point.dtd"[]>
<Identity created="1525465321820" name="Onboarding - GUI - External">
<Attributes>
<Map>
<entry key="displayName" value="Onboarding - GUI " />
<entry key="firstname" value="Z Orphaned ID" />
</Map>
</Attributes>
</Identity>
"@
我不知道这是否是您预期的输出,但错误是由空参数引起的。要保证需要$xml
,您可以执行以下操作:
function Get-XmlNamespaceManager([parameter(Mandatory=$true)][xml]$xml, [string]$NamespaceURI = "")
{
if ([string]::IsNullOrEmpty($NamespaceURI)) {$NamespaceURI = $xml.DocumentElement.NamespaceURI}
[System.Xml.XmlNamespaceManager]$xmlNsManager = New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
$xmlNsManager.AddNamespace("ns", $NamespaceURI)
return ,$xmlNsManager
}
所以现在如果我这样做了
Get-XmlNamespaceManager
它会问我参数
cmdlet Get-XmlNamespaceManager at command pipeline position 1
Supply values for the following parameters:
xml:
如果我没有通过按[code>enter在那里输入参数,这将传递
$xml
作为“
”Hi-Neko,感谢您的帮助和解释,您对这是我的预期输出,但没有在管道上输入xml值。它将返回输出,并将在另一个函数中使用。它不会在另一个函数中使用。只是您的函数似乎总是返回相同的内容。请尝试使用新窗口或powershell
命令重置powershell作用域,然后再次运行该函数。它将是相同的。在没有$global:
的函数中定义的所有变量不会在其外部环境中更改。例如,创建函数function myfunc{$test=“test”}
并运行它,查看$test
在函数外部是否可用,然后尝试使用function myfunc{$global:test=“test”}
,并查看是否可以在函数外部使用$test
。
function Get-XmlNamespaceManager([parameter(Mandatory=$true)][xml]$xml, [string]$NamespaceURI = "")
{
if ([string]::IsNullOrEmpty($NamespaceURI)) {$NamespaceURI = $xml.DocumentElement.NamespaceURI}
[System.Xml.XmlNamespaceManager]$xmlNsManager = New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
$xmlNsManager.AddNamespace("ns", $NamespaceURI)
return ,$xmlNsManager
}
Get-XmlNamespaceManager
cmdlet Get-XmlNamespaceManager at command pipeline position 1
Supply values for the following parameters:
xml: