簡體   English   中英

Powershell XML屬性名稱空間

[英]Powershell XML attribute 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>

我想自動生成它,其方式與當前使用以下測試代碼顯示的方式相同

[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)

但是我得到的輸出是

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

我如何:

A)擺脫名稱空間文本

xmlns:system=" xmlns xml x system"

B)在我的屬性前面加上“ x:”

任何幫助,將不勝感激。

問候,

瑞安

您需要在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")

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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM