添加位于多级的 XML 内容 - PowerShell 函数

Posted

技术标签:

【中文标题】添加位于多级的 XML 内容 - PowerShell 函数【英文标题】:Add XML content located on multi level - PowerShell function 【发布时间】:2022-01-03 02:27:00 【问题描述】:

我的工作需要从用户数据输入更新 xml 文件。

问题总结:

经过 1 天的完整试用后,我未能成功创建 PowerShell 函数,允许我在用户数据的“依赖项”元素下插入任意数量的“依赖项”元素。与“先决条件”元素下的“先决条件”相同。

我的研究人员:

add multi level xml powershell adding XML sub-elements

我最有前途的代码:

```
# load XML file
[xml]$doc = Get-Content "D:\TEMP\INPUT.xml"


# create node <Dependency>
$sref = $doc.CreateNode('element', 'Dependency', '')
$sref.SetAttribute("id", "GGG")
$sref.SetAttribute("version", "HHH")

# create node <Dependencies> and append child nodes <id> and <version>
$src = $doc.CreateNode('element', 'Dependencies', '')
#$src.AppendChild($comp)
$src.AppendChild($sref)

# append node <Source> to node <metadata>
$svc = $doc.SelectSingleNode('//metadata')
$svc.AppendChild($src)

# save XML file
$doc.Save("D:\TEMP\OUTPUT.xml")
```

我的原始 xml 文件:

```
<?xml version="1.0" encoding="utf-8"?>
<package>
    <metadata>
        <id>AAA</id>
        <version>BBB</version>
        <authors>CCC</authors>
        <owners>XXX</owners>
        <description>EEE</description>
        <keyfile>DDD</keyfile>
        <dependencies />
        <prerequisites />
        <librairies />
        <rules />
    </metadata>
    <files />
</package>
```

预期的 XML 文件:

```
<?xml version="1.0" encoding="utf-8"?>
<package>
  <metadata>
    <id>AAA</id>
    <version>BBB</version>
    <authors>CCC</authors>
    <owners>XXX</owners>
    <description>EEE</description>
    <keyfile>DDD</keyfile>
    <dependencies>
      <dependency id="GGG" version="HHH" />
      <dependency id="III" version="JJJ" />
      <dependency id="KKK" version="LLL" />
    </dependencies>
    <prerequisites>
      <dependency id="UUU" version="VVV" />
      <dependency id="WWW" version="XXX" />
    </prerequisites>
    <librairies />
    <rules />
  </metadata>
  <files />
</package>
```

用户数据输入:

```
For "dependency" :
   - Parameter set 1 :
      - GGG
      - HHH
   - Parameter set 2 :
      - III
      - JJJ
   - Parameter set 3 :
      - KKK
      - LLL

For "prerequisite" :
   - Parameter set 1 :
      - UUU
      - VVV
   - Parameter set 2 :
      - WWW
      - XXX
```

【问题讨论】:

你能贴出你到目前为止写的任何代码吗? 我已经编辑了我的帖子 【参考方案1】:

您已经非常接近了,但您不需要创建自己的 &lt;dependencies&gt; 元素 - 您只需要在原始文档中获取对那个元素的引用...

$doc = [xml] @"
<?xml version="1.0" encoding="utf-8"?>
<package>
  <metadata>
    <id>AAA</id>
    <version>BBB</version>
    <authors>CCC</authors>
    <owners>XXX</owners>
    <description>EEE</description>
    <keyfile>DDD</keyfile>
    <dependencies />
    <prerequisites />
    <librairies />
    <rules />
  </metadata>
  <files />
</package>
"@;

# create new "<dependency>" element
$dependency = $doc.CreateNode("element", "dependency", "");
$dependency.SetAttribute("id", "GGG");
$dependency.SetAttribute("version", "HHH");

# add it to the existing "<dependencies>" element
$dependencies = $doc.SelectSingleNode("package/metadata/dependencies");
$dependencies.AppendChild($dependency);

# save XML file
$doc.Save("D:\TEMP\OUTPUT.xml")
# <?xml version="1.0" encoding="utf-8"?><package><metadata><id>AAA</id><version>BBB</version><authors>CCC</authors><owners>XXX</owners><description>EEE</description><keyfile>DDD</keyfile><dependencies><dependency id="GGG" version="HHH" /></dependencies><prerequisites /><librairies /><rules /></metadata><files /></package>

如果您想要带有换行符和缩进的漂亮打印的 xml,您可以这样做来保存 xml:

# see https://devblogs.microsoft.com/powershell/format-xml/
function Format-XML ([xml]$xml, $indent=2)

    $StringWriter = New-Object System.IO.StringWriter
    $XmlWriter = New-Object System.XMl.XmlTextWriter $StringWriter
    $xmlWriter.Formatting = "indented"
    $xmlWriter.Indentation = $Indent
    $xml.WriteContentTo($XmlWriter)
    $XmlWriter.Flush()
    $StringWriter.Flush()
    Write-Output $StringWriter.ToString()


Set-Content -Path "D:\TEMP\OUTPUT.xml" -Value (Format-XML $doc);

#<?xml version="1.0" encoding="utf-8"?>
#<package>
#  <metadata>
#    <id>AAA</id>
#    <version>BBB</version>
#    <authors>CCC</authors>
#    <owners>XXX</owners>
#    <description>EEE</description>
#    <keyfile>DDD</keyfile>
#    <dependencies>
#      <dependency id="GGG" version="HHH" />
#    </dependencies>
#    <prerequisites />
#    <librairies />
#    <rules />
#  </metadata>
#  <files />
#</package>

【讨论】:

以上是关于添加位于多级的 XML 内容 - PowerShell 函数的主要内容,如果未能解决你的问题,请参考以下文章

PowerShe 命令行调试指引(转)

vue+router多级嵌套路由,路径正确但显示内容不对

PowerShe 使用证书签名 ll脚本

多级评论

c#读取XML多级子节点

按多级属性值选择 XML 节点值