![](/img/trans.png)
[英]Setting xml value in powershell failing with Property '…' cannot be found on this object
[英]Powershell New-Object : A constructor was not found
我正在嘗試創建一個 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
}
在消息之后:
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}}
New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
"~~~~~~~~~~~~~~~"
那是特定錯誤的位置。 這沒有具體的錯誤,但它只是因為您如何調用 function 而成為錯誤。
$xmlNsManager
您可能知道(因為您制作了 function 我假設) function 需要兩個參數,( $xml
和$namespaceuri
)。 $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^
)
如果我運行 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>
"@
我沒有收到錯誤,而是收到 output:
xmlns
xml
ns
不知道這是不是你想要的output,但是錯誤是因為參數為空造成的。 為保證需要$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:
如果我沒有按enter
在那里輸入參數,這會將$xml
作為""
傳遞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.