我需要在 xml 文档中再添加 3 个节点。我正在尝试以多种方式解决问题,但不幸的是没有结果我的代码正在完成这项工作,但正在添加一个我不想要的属性:如何重复节点?
My code:
function Get-XmlNamespaceManager([ xml ]$XmlDocument, [string]$NamespaceURI = "")
{
# If a Namespace URI was not given, use the Xml document's default namespace.
if ([string]::IsNullOrEmpty($NamespaceURI)) { $NamespaceURI = $XmlDocument.DocumentElement.NamespaceURI }
# In order for SelectSingleNode() to actually work, we need to use the fully qualified node path along with an Xml Namespace Manager, so set them up.
[System.Xml.XmlNamespaceManager]$xmlNsManager = New-Object System.Xml.XmlNamespaceManager($XmlDocument.NameTable)
$xmlNsManager.AddNamespace("ns", $NamespaceURI)
return ,$xmlNsManager # Need to put the comma before the variable name so that PowerShell doesn't convert it into an Object[].
}
function Get-FullyQualifiedXmlNodePath([string]$NodePath, [string]$NodeSeparatorCharacter = '.')
{
return "/ns:$($NodePath.Replace($($NodeSeparatorCharacter), '/ns:'))"
}
function Get-XmlNode([ xml ]$XmlDocument, [string]$NodePath, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.')
{
$xmlNsManager = Get-XmlNamespaceManager -XmlDocument $XmlDocument -NamespaceURI $NamespaceURI
[string]$fullyQualifiedNodePath = Get-FullyQualifiedXmlNodePath -NodePath $NodePath -NodeSeparatorCharacter $NodeSeparatorCharacter
# Try and get the node, then return it. Returns $null if the node was not found.
$node = $XmlDocument.SelectSingleNode($fullyQualifiedNodePath, $xmlNsManager)
return $node
}
function Get-XmlNodes([ xml ]$XmlDocument, [string]$NodePath, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.')
{
$xmlNsManager = Get-XmlNamespaceManager -XmlDocument $XmlDocument -NamespaceURI $NamespaceURI
[string]$fullyQualifiedNodePath = Get-FullyQualifiedXmlNodePath -NodePath $NodePath -NodeSeparatorCharacter $NodeSeparatorCharacter
# Try and get the nodes, then return them. Returns $null if no nodes were found.
$nodes = $XmlDocument.SelectNodes($fullyQualifiedNodePath, $xmlNsManager)
return $nodes
}
$XmlDocument = Get-ChildItem -Path 'C:\Scripts\Add_Languages\Source\' -Recurse -Include "*.imdi" -File | ForEach-Object{
[xml]$Xml = Get-Content $_.FullName
$Keynodes = Get-XmlNodes $Xml -NodePath "METATRANSCRIPT.Session.MDGroup.Content.Keys"
$ref = $Xml.METATRANSCRIPT.Session.MDGroup.Content.Keys.Key | Where {$_.Name -eq 'Topic'}
$Key = $Xml.CreateElement('Key')
$addAtt = $xml.CreateAttribute("Name")
$addAtt.Value = "Topic"
$key.Attributes.Append($addAtt)
$Keynodes.InsertAfter($Key,$ref)
$ref = $Xml.METATRANSCRIPT.Session.MDGroup.Content.Keys.Key | Where {$_.Name -eq 'Keyword'}
$Key = $Xml.CreateElement('Key')
$addAtt = $xml.CreateAttribute("Name")
$addAtt.Value = "Keyword"
$key.Attributes.Append($addAtt)
$Keynodes.InsertAfter($Key,$ref)
$xml.Save($_.FullName)
}
这是我的代码的结果:
<Keys>
<Key Name="Status">Finished</Key>
<Key Name="Keyword">Language profile, interview, multilingualism, younger speaker</Key>
<Key Name="Topic">Language profile</Key>
<Key Name="Topic" xmlns="" /> # <<<<< This node, it's adding the xmlns="" (i don´t need) i need only <Key Name="Topic"/>
<Key Name="Location Address">Kabaroan, Babuyan Claro</Key>
</Keys>
我需要:
<Keys>
<Key Name="Status">Finished</Key>
<Key Name="Keyword">Language profile, interview, multilingualism, younger speaker</Key>
<Key Name="Topic">Language profile</Key>
<Key Name="Topic"></Key>
<Key Name="Topic"></Key>
<Key Name="Topic"></Key>
<Key Name="Location Address">Kabaroan, Babuyan Claro</Key>
</Keys>
再次感谢您在此问题上的任何帮助。