Powershell XML Attribute Namespaces

I have the following stub 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 bind to automatically generate it in the same way as it currently displays 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 conclusion that I get 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> 

Like me:

A) get rid of namespace text

 xmlns:system=" xmlns xml x system" 

B) my Attributes prefix with "x:"

Any help would be appreciated.

Hello,

Ryan

+4
source share
1 answer

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

 $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> 
+6
source

Source: https://habr.com/ru/post/1483349/


All Articles