没有 InnerText 的 Powershell XML 解析问题?

发布于 2024-12-28 07:19:30 字数 1534 浏览 0 评论 0原文

这应该很容易完成,但我在编写 powershell 脚本来编辑由 HP 团队软件导出功能生成的 XML 文件时遇到问题。我认为问题源于创建的元素的唯一内容是子元素。

我在错误的位置遇到了 AppendChild 的问题,并且 InnerTexts 在 vlan 元素下为空。有人可以帮助我完成将这 4 行 xml 添加到下面的 team.xml 所需的 powershell xml 吗?

<vlan>
    <property id='VlanId' value='3'/>
    <property id='VlanName' value='MISC'/>
</vlan>

以下是 team.xml 的缩写内容:

<?xml version="1.0" encoding="ISO-8859-1"?>
<teamingconfig>
<version UtilityVersion='9.90.0.17' ScriptVersion='3.1'/>
<!-- <team> element 1 -->
<team relnics='1 2'>
<property id='TeamName' value='HP Network Team #1'/>
<property id='OpMode' value='FailOnFault'/>
<!-- <vlan> element 1 -->
<vlan>
       <property id='VlanId' value='1'/>
       <property id='VlanName' value='MGMT'/>
</vlan>
  <!-- <vlan> element 2 -->
<vlan>
       <property id='VlanId' value='2'/>
       <property id='VlanName' value='APPS'/>
</vlan>
</team>
</teamingconfig>

抱歉进行了多次编辑- 得到了我想要的。语法有点奇怪-

$vlan2=$xml.SelectSingleNode("//property[@value='407'] [@id='VlanId']")
$vlan2.value="100"

新问题,为什么如果我在脚本顶部添加一行:

write-host "Num Args:" $args.length

然后向 ps1 传递一个变量,它似乎与 SelectSingleNode 混淆,说 system.object[] 不包含名为 selectsinglenode 的方法?我没有使用参数来触及 $xml 变量。例如,我基本上试图让 vlanid 作为命令行参数。我尝试过创建一个函数并使用参数,但它在代码中产生了同样的问题。

明白了 - $xmldata = xml 感谢大家!

This should be rather easily accomplished but I am having issues writing a powershell script to edit an XML file generated by HP's teaming software export function. I believe the issue stems from creating elements who's only contents are sub elements.

I am struggling with AppendChild in the wrong spot, and InnerTexts being empty under the vlan element. Can someone help me with the powershell xml necessary to accomplish adding these 4 lines of xml to the team.xml below?

<vlan>
    <property id='VlanId' value='3'/>
    <property id='VlanName' value='MISC'/>
</vlan>

Here is the abbreviated content of team.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<teamingconfig>
<version UtilityVersion='9.90.0.17' ScriptVersion='3.1'/>
<!-- <team> element 1 -->
<team relnics='1 2'>
<property id='TeamName' value='HP Network Team #1'/>
<property id='OpMode' value='FailOnFault'/>
<!-- <vlan> element 1 -->
<vlan>
       <property id='VlanId' value='1'/>
       <property id='VlanName' value='MGMT'/>
</vlan>
  <!-- <vlan> element 2 -->
<vlan>
       <property id='VlanId' value='2'/>
       <property id='VlanName' value='APPS'/>
</vlan>
</team>
</teamingconfig>

Sorry for the multiple edits-
Got what I wanted. Syntax a little strange-

$vlan2=$xml.SelectSingleNode("//property[@value='407'] [@id='VlanId']")
$vlan2.value="100"

New issue, how come if I add a line like at the top of my script:

write-host "Num Args:" $args.length

and then pass the ps1 a variable, it seems to mess with the SelectSingleNode saying the system.object[] doesn't contain a method named selectsinglenode? I am not touching the $xml variable with the args. I am basically trying to just have the vlanid be a command line argument for instance. I have tried making a function and using param but it yields the same problem with the code.

Got it- $xmldata = xml
Thanks to all!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦行七里 2025-01-04 07:19:30

您可以克隆一个并将其添加回来:

$vlan = $xml.SelectSingleNode("//vlan").clone()
$vlan.property[0].value = "3"
$vlan.property[1].value = "MISC"
[void]$xml.SelectSingleNode("//team").AppendChild($vlan)
$xml.save("test.xml")

You can clone one and add it back:

$vlan = $xml.SelectSingleNode("//vlan").clone()
$vlan.property[0].value = "3"
$vlan.property[1].value = "MISC"
[void]$xml.SelectSingleNode("//team").AppendChild($vlan)
$xml.save("test.xml")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文