Powershell XML属性名称空间

Rya*_*yan 4 xml powershell namespaces

我有以下存根XML文件

<ResourceDictionary x:Uid="CultureResources" 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:system="clr-namespace:System;assembly=mscorlib" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

 <system:String x:Uid="system::.CultureName" x:Key=".CultureName">de-DE</system:String>

</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

我想以与当前使用以下测试代码一样的方式自动生成它

[xml]$xmlfile = Get-Content "c:\test.xml"

[System.Xml.XmlNamespaceManager] $nsm = new-object System.Xml.XmlNamespaceManager $xmlfile.NameTable
$nsm.AddNamespace("x", "http://schemas.microsoft.com/winfx/2006/xaml")
$nsm.AddNamespace("system", "clr-namespace:System;assembly=mscorlib")

$nn = $xmlfile.CreateElement("system:String", $nsm)
$nn.set_InnerText("de-DE")
$nn.SetAttribute("Uid","system:.CultureName")
$nn.SetAttribute("Key",".CultureName")
$xmlfile.ResourceDictionary.AppendChild($nn)
Run Code Online (Sandbox Code Playgroud)

但我得到的输出是

<ResourceDictionary x:Uid="CultureResources" 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:system="clr-namespace:System;assembly=mscorlib" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

 <system:String x:Uid="system::.CultureName" x:Key=".CultureName">de-DE</system:String>
 <system:String Uid="system:.CultureName" Key=".CultureName" xmlns:system=" xmlns xml x system">de-DE</system:String>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

我如何能:

A)摆脱命名空间文本

xmlns:system=" xmlns xml x system"
Run Code Online (Sandbox Code Playgroud)

B)使用"x:"为我的属性添加前缀

任何帮助,将不胜感激.

问候,

瑞安

Fro*_* F. 6

您需要使用另一个重载createelement()setattribute()指定namespaceURI.获得uri的一个干净方法是从$nms需要时获取uri .试试这个:

$xml = [xml]@"
<ResourceDictionary x:Uid="CultureResources" 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:system="clr-namespace:System;assembly=mscorlib" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

 <system:String x:Uid="system::.CultureName" x:Key=".CultureName">de-DE</system:String>

</ResourceDictionary>
"@

[System.Xml.XmlNamespaceManager] $nsm = new-object System.Xml.XmlNamespaceManager $xml.NameTable
$nsm.AddNamespace("x", "http://schemas.microsoft.com/winfx/2006/xaml")
$nsm.AddNamespace("system", "clr-namespace:System;assembly=mscorlib")


#$nn = $xml.CreateElement("system", "String", $nsm.LookupNamespace("system"))  this works too:  prefix, name, NSuri
$nn = $xml.CreateElement("system:String", $nsm.LookupNamespace("system"))
$nn.set_InnerText("de-DE")
$nn.SetAttribute("Uid", $nsm.LookupNamespace("x"), "system::.CultureName")
$nn.SetAttribute("Key", $nsm.LookupNamespace("x"), ".CultureName")
$xml.ResourceDictionary.AppendChild($nn)
$xml.Save("C:\users\graimer\Desktop\test.xml")
Run Code Online (Sandbox Code Playgroud)

的test.xml

<ResourceDictionary x:Uid="CultureResources" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:system="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <system:String x:Uid="system::.CultureName" x:Key=".CultureName">de-DE</system:String>
  <system:String x:Uid="system::.CultureName" x:Key=".CultureName">de-DE</system:String>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)