【发布时间】:2014-04-08 17:33:03
【问题描述】:
我有这个 XML:
<?xml version="1.0" encoding="utf-8"?>
<Man schemaVersion="1">
<version>2.2</version>
<file>
<properties>
…
</properties>
<group>
<properties>
...
</properties>
</group>
<group>
<properties>
…
</properties>
<group>
<properties>
<items>
<name>test</name>
<description>A test</description>
</items>
</properties>
</group>
<group>
<properties>
<items>
<name>test2</name>
<description>A test field again</description>
</items>
</properties>
</group>
</group>
</file>
</Man>
我想用powershell添加一个子节点(?)包含以下内容:
<group>
<properties>
<items>
<name>test3</name>
<description>one more field</description>
</items>
</properties>
</group>
正好在 test2 节点下,看起来像这样:
<group>
<properties>
<items>
<name>test</name>
<description>A test</description>
</items>
</properties>
</group>
<group>
<properties>
<items>
<name>test2</name>
<description>A test field again</description>
</items>
</properties>
</group>
<group>
<properties>
<items>
<name>test3</name>
<description>one more field</description>
</items>
</properties>
</group>
问题是“组”被识别为数组,我无法向数组添加元素。我尝试了很多技巧来添加元素但不在数组中
代码(正如我所说,它处于非常初级的阶段,我首先关心的是获得一些测试值。
$strXMLfile="c:\blabla\bla.xml";
$xml=get-content $strXMLfile;
$xmlRoot=$xml.get_DocumentElement();
$xmlnode=$xmlRoot.file.group;
$group=0;
while (!($xmlnode[$group].properties.name -eq "test node")) {
$group++
}
$nodesGroups=$xmlnode[$group].group;
if ($nodesGroups.count -eq $null) {
$intNewGroup=2
}
else {
$intNewGroup=$nodesGroups.count+1
}
#Here should be the code for the group,properties,items line creation. Partially
$newline=$xml.CreateElement("items")
#Here should be the code for the <name>. Not ready yet
【问题讨论】:
-
我从 XML 中添加了一些内容,以注意外部
不止一个。
标签: xml powershell