没有 InnerText 的 Powershell XML 解析问题?
这应该很容易完成,但我在编写 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以克隆一个并将其添加回来:
You can clone one and add it back: