1

我很难找到任何信息来帮助我弄清楚如何将 XML 命名空间从一个 XML 文档添加到另一个文档。我尝试使用 System.XML.XmlNamespaceManager 类无济于事。例如,假设我有一个这样的 XML 文档:

<mso:customUI xmlns:x2="DMOLAddin" xmlns:x1="MimecastServicesForOutlook.AddinModule" xmlns:mso="http://schemas.microsoft.com/office/2009/07/customui">
<mso:ribbon>
foo
</mso:customUI>

我还有另一个这样的 XML 文档:

<mso:customUI xmlns:x3="crmaddin.RibbonAddin" xmlns:x2="DMOLAddin" xmlns:x1="MimecastServicesForOutlook.AddinModule" xmlns:mso="http://schemas.microsoft.com/office/2009/07/customui">
<mso:ribbon>
Bar
</mso:ribbon>
</mso:customUI>

我将如何枚举文档一中的命名空间并仅将那些尚不存在的文档添加到文档二中?

4

1 回答 1

0

像这样的东西会起作用吗?

[xml]$xml1 = @"
<mso:customUI xmlns:x2="DMOLAddin" xmlns:x1="MimecastServicesForOutlook.AddinModule" xmlns:mso="http://schemas.microsoft.com/office/2009/07/customui">
<mso:ribbon>
foo
</mso:ribbon>
</mso:customUI>
"@

[xml]$xml2 = @"
<mso:customUI xmlns:x3="crmaddin.RibbonAddin" xmlns:x2="DMOLAddin" xmlns:x1="MimecastServicesForOutlook.AddinModule" xmlns:mso="http://schemas.microsoft.com/office/2009/07/customui">
<mso:ribbon>
Bar
</mso:ribbon>
</mso:customUI>
"@

$ns1 =  $xml1.customUI.Attributes |
        Where-Object { $_.Prefix -eq 'xmlns' }
$ns2 =  $xml2.customUI.Attributes |
        Where-Object { $_.Prefix -eq 'xmlns' } |
        ForEach-Object { $_.LocalName }

$ns1 | Where-Object {
    $ns2 -notcontains $_.LocalName
} | ForEach-Object {
    $xml2.customUI.SetAttribute($_.Name, $_.Value)
}
于 2013-09-09T21:47:30.430 回答