我有以下存根
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"
任何帮助,将不胜感激.
问候,
瑞安
解决方法
您需要在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","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",".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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。