简体   繁体   English

Powershell和XML子节点

[英]Powershell and XML subnodes

I have this XML: 我有这个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>

I want with powershell to add one subnode(?) contain the follows: 我想用powershell添加一个包含以下内容的子节点(?):

    <group>
        <properties>
            <items>
                <name>test3</name>
                <description>one more field</description>
            </items>
        </properties>
    </group>

exactly under the test2 node so that looks like this: 正好在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>

The problem is that the "group" is being recognized as array and I'm not able to add an element to the array. 问题是该“组”被识别为数组,而我无法向该数组添加元素。 I have try a lot of tricks to add element but not in the array 我尝试了很多技巧来添加元素,但不在数组中

The code (as I said it is in very primary stage, my first concern was to get to the with some test values. 代码(正如我所说的,它处于非常初级的阶段,我首先要考虑的是获得一些测试值。

$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

You can add a new last child to an existing element (type XmlElement ) with the AppendChild method. 您可以使用AppendChild方法将新的最后一个子级添加到现有元素(类型XmlElement )。 So rather than manipulating the array of <group> elements, manipulate their common parent (the <file> element). 因此,与其操纵<group>元素的数组,不如操纵它们的公共父元素( <file>元素)。

Hopefully this can get you started: 希望这可以帮助您入门:

 > $xml = [XML]"<Man>...</Man>"
 > $newGrp = $xml.CreateElement("group")
 > $newGrp.InnerText = "group 3"
 > $xml.Man.file.group.AppendChild($newGrp)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM