【问题标题】:PowerShell manipulating XML - Adding arbitrary data to XML ObjectPowerShell 操作 XML - 向 XML 对象添加任意数据
【发布时间】:2019-11-18 08:11:56
【问题描述】:

这是 API 调用的预定义变量。

$CorePluginConfigurationContext = ([xml]"
<CorePluginConfigurationContext xmlns='http://schemas.solarwinds.com/2012/Orion/Core' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>
    <BulkList>
        <IpAddress>
            <Address></Address>
        </IpAddress>
    </BulkList>
</CorePluginConfigurationContext>
").DocumentElement

阅读 PowerShell 书籍、在线文档、XML 命名空间、Nodes、Parents、Childs、Appending、“element”“s”,我根本无法向这个特定变量添加数据。

我得到多个值作为参数

Param(
    [Parameter(Mandatory=$true)]
    [String[]]$nodes = "1.1.1.1"
)

并且需要将这些值添加到 XML 对象中。

foreach ($node in $nodes) { }

结果会是这样的:

<CorePluginConfigurationContext xmlns='http://schemas.solarwinds.com/2012/Orion/Core' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>
    <BulkList>
        <IpAddress>
            <Address>10.10.1.19</Address>
        </IpAddress>
        <IpAddress>
            <Address>10.10.1.20</Address>
        </IpAddress>
    </BulkList>
</CorePluginConfigurationContext>

测试代码:

Param (
    [Parameter(Mandatory=$true)]
    [String[]]$nodes = "1.1.1.1"
)

$CorePluginConfigurationContext = ([xml]"
<CorePluginConfigurationContext xmlns='http://schemas.solarwinds.com/2012/Orion/Core' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>
    <BulkList>
        <IpAddress>
            <Address></Address>
        </IpAddress>
    </BulkList>
</CorePluginConfigurationContext>
").DocumentElement

foreach ($node in $nodes) {
    # Code to add multiple $node values to the XML above
}

【问题讨论】:

  • 很遗憾你没有发布你已经尝试过的代码,所以很难说你的问题是什么。所以请edit您的问题并添加您的代码。同时也许this might be helpful

标签: xml powershell


【解决方案1】:

一方面,您应该将 XML document 分配给您的变量,而不仅仅是 document 元素(即根节点):

$xml = [xml]"
<CorePluginConfigurationContext xmlns='http://schemas.solarwinds.com/2012/Orion/Core' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>
    <BulkList>
        <IpAddress>
            <Address></Address>
        </IpAddress>
    </BulkList>
</CorePluginConfigurationContext>"

这不是什么大问题,因为您可以通过$xml.OwnerDocument 从节点获取文档,但最好让变量保存实际的文档对象。

此外,如果您打算稍后添加数据,则不应将空节点放入文档中,因为您必须删除空节点或分配与所有其他值不同的值之一。

接下来,您的 XML 文档具有名称空间,因此您需要一个名称空间管理器。

$uri = 'http://schemas.solarwinds.com/2012/Orion/Core'
$nm = New-Object Xml.XmlNamespaceManager $xml.NameTable
$nm.AddNamespace('ns', $uri)

使用该命名空间管理器,您可以选择要附加到的节点:

$bulklist = $xml.SelectSingleNode('ns:BulkList', $nm)

然后遍历您的输入值,为它们创建节点,并将它们附加到选定的父节点:

foreach ($node in $nodes) {
    $addr = $xml.CreateElement('Address', $uri)
    $addr.InnerText = $node

    $ip = $xml.CreateElement('IpAddress', $uri)
    [void]$ip.AppendChild($addr)

    [void]$bulklist.AppendChild($ip)
}

【讨论】:

  • 感谢您指出正确的方向,我知道命名空间是我需要关注的事情。我仍然不明白“BulkList”是否是命名空间。
  • @OSB &lt;BulkList&gt; 是一个节点,而不是一个命名空间。命名空间是通过 xmlns 属性定义的,并通过在节点名称前加上命名空间名称 (&lt;namespace:node&gt;) 来使用。只有默认命名空间(通过xmlns= 定义的命名空间不带前缀。Related
  • 没用,还不知道怎么构造node和childs
  • “无效”不是有效的问题描述。构建节点的代码在我的回答中。
【解决方案2】:

使用下面,我能够做我想做的事。

$bulk = $CorePluginConfigurationContext.BulkList
$ipAdd = $bulk.IpAddress.Clone()
$bulk.IpAddress.Address = $nodes[0]
for ($i = 1; $i -lt $nodes.Length; $i++)
{
    $node = $nodes[$i]
    $another = $ipAdd.Clone()
    $another.Address = $node
    [void]$bulk.AppendChild($another)
}

【讨论】:

    猜你喜欢
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-11
    • 2011-08-17
    • 1970-01-01
    相关资源
    最近更新 更多