Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell XML attribute namespaces

I have the following stub XML file

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

I am tying to auto generate it in the same manner it is currently displayed with the following test code

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

But the output I am getting is

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

How do I:

A) get rid of the namespace text

xmlns:system=" xmlns xml x system"

B) prefix my Attributes with "x:"

Any help would be appreciated.

Regards,

Ryan

like image 268
Ryan Avatar asked May 29 '13 11:05

Ryan


1 Answers

You need to use another overload on createelement() and setattribute() that specifies namespaceURI. A clean way to get the uri would be to get the uri from $nms when you need it. Try this:

$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>
like image 101
Frode F. Avatar answered Sep 21 '22 20:09

Frode F.