4

其实我对xtd-transform了解不多。我在 xdt 文件中需要什么:

<add key="EndpointName" value="SomeValue" xdt:Transform="SetAttributes(value)" xdt:Locator="Match(key)" />

我在 powershell 中做的日常工作:

$cache.SetAttribute("key","EndpointName")
$cache.SetAttribute("value","SomeValue")
$cache.SetAttribute("xdt:Transform","SetAttributes(value)")
$cache.SetAttribute("xdt:Locator","Match(key)")

这就是我所拥有的。不符合我的观点:

<add key="EndpointName" value="Email" Transform="SetAttributes(value)" Locator="Match(key)" />

那么是否可以使用 powershell 脚本创建 xdt:attribute ?

多谢你们!

4

1 回答 1

6

当涉及 XML 命名空间时,您需要使用 XmlNamespaceManager 例如:

$xdt = 'http://schemas.microsoft.com/XML-Document-Transform'
$xml = [xml]"<doc xmlns:xdt='$xdt'><add key='foo' value='foo' xdt:Transform='foo' xdt:Locator='foo'/></doc>"

$nsmgr = new-object Xml.XmlNamespaceManager $xml.NameTable
$nsmgr.AddNamespace("xdt", $xdt)

$xml.doc.add.SetAttribute('Transform', $xdt, 'SetAttribute(value)') > $null

结果是:

<doc xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <add key="foo" value="foo" xdt:Transform="SetAttribute(value)" xdt:Locator="foo" />
</doc>
于 2012-10-18T17:41:46.087 回答