2013-05-29 190 views
4

我有以下存根XML文件PowerShell的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:”

任何帮助,将不胜感激。

问候,

瑞安

回答

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

的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> 
+0

优秀的感谢,我只是拿出我自己的代码,并没有公布这几乎是为你所做的只是一些额外的相同因为我不知道你使用过的一些简短表格。 另一个令人困惑的地方是“xmlns:system =”xmlns xml x system“”出现,但这是由于我在查看“outertext”字段并假设它是实际输出。 – Ryan

+0

外部xml等于acutal输出。你得到xmlns部分的原因是你使用了过载错误。你给namespaceuri-paramter'$ nms'作为值,而不是uri本身。 '$ nms'是一个管理器,所以当你使用它作为输入时,你得到它的名字空间(xmlns,xml,x,system)作为命名空间uri。 –