【问题标题】:Powershell XML attribute namespacesPowershell XML 属性命名空间
【发布时间】:2013-05-29 11:50:35
【问题描述】:

我有以下存根 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:”

任何帮助将不胜感激。

问候,

瑞恩

【问题讨论】:

    标签: xml powershell namespaces


    【解决方案1】:

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

    【讨论】:

    • 非常感谢,我只是想出了我自己的代码,并没有发布它几乎和你做了几行一样,因为我不知道一些较短的您使用过的表格。另一个令人困惑的地方是出现了“xmlns:system="xmlns xml x system"”,但这是由于我查看了“outertext”字段并假设这是实际输出。
    • outerxml 等于实际输出。你得到 xmlns 部分的原因是你使用了错误的重载。您将 namespaceuri-paramter $nms 作为值,而不是 uri 本身。 $nms 是一个管理器,所以当你将它用作输入时,你会得到它的命名空间(xmlns、xml、x、system)作为命名空间 uri。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-27
    • 2019-05-06
    • 2013-09-21
    • 1970-01-01
    • 1970-01-01
    • 2018-07-12
    • 1970-01-01
    相关资源
    最近更新 更多