简体   繁体   English

Powershell 新对象:未找到构造函数

[英]Powershell New-Object : A constructor was not found

I`m trying to create a function to get de NameSpaceManager, but i'm get a message error and a don't understand why.我正在尝试创建一个 function 来获取名称空间管理器,但我收到一条消息错误并且不明白为什么。

 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   

} }

Following the message:在消息之后:

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

But My $xmlNsManager returns:但我的 $xmlNsManager 返回:

PS C:\Path\To\Myfiles> $xmlNsManager

xmlns xml ns xmlns xml ns

Thanks for an help.感谢您的帮助。

This is part of my code:这是我的代码的一部分:

 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}}
New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
                                         "~~~~~~~~~~~~~~~"

That is the location for the specific error.那是特定错误的位置。 There isn't an error with this specifically, but it only became an error because of how you called the function.这没有具体的错误,但它只是因为您如何调用 function 而成为错误。

$xmlNsManager

As you probably know (since you made the function I presume) the function takes two parameters, ( $xml and $namespaceuri ).您可能知道(因为您制作了 function 我假设) function 需要两个参数,( $xml$namespaceuri )。 $namespaceuri can be null and is not mandatory, however though you haven't specified that $xml is mandatory, it is since of the things you have to do with it. $namespaceuri可以是null并且不是强制性的,但是尽管您没有指定$xml是强制性的,但这是因为您必须使用它。 ( (

if ([string]::IsNullOrEmpty($NamespaceURI)) {$NamespaceURI = $xml.DocumentElement.NamespaceURI}
#                                                          here ^
[System.Xml.XmlNamespaceManager]$xmlNsManager = New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
#                                                                                   and here^

) )

If i run the function but I put the $xml parameter like so:如果我运行 function 但我像这样放置$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>
"@

I don't get an error, instead I get the output:我没有收到错误,而是收到 output:

xmlns
xml
ns

I don't know if this is your intended output, but the error is caused because of the empty parameters.不知道这是不是你想要的output,但是错误是因为参数为空造成的。 To guarantee that $xml is required, you can do:为保证需要$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   
}

So now if I do所以现在如果我这样做

Get-XmlNamespaceManager

It will ask me for parameters它会问我参数

cmdlet Get-XmlNamespaceManager at command pipeline position 1
Supply values for the following parameters:
xml:

And if I don't enter the parameters there by pressing enter , this will pass $xml as "" .如果我没有按enter在那里输入参数,这会将$xml作为""传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM