简体   繁体   中英

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 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:

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

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.

$xmlNsManager

As you probably know (since you made the function I presume) the function takes two parameters, ( $xml and $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. (

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:

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:

xmlns
xml
ns

I don't know if this is your intended output, but the error is caused because of the empty parameters. To guarantee that $xml is required, you can do:

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 "" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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