Powershell:在特定的当前同级元素之后添加新的 XML 元素 [英] Powershell : Add new XML element after a specific current sibling element
问题描述
我有一个 xml 列表,我希望在
下添加一个新的子元素.我已经能够将子元素附加到游戏节点块的末尾,但为了命名标准,我需要将它作为图像的兄弟元素插入到图像元素之后.
I have an xml list that I wish to add a new child element to under <game>
. I have been able to appended the child element to the end of the game node block but for naming standards I need to insert it after the image element as a sibling of image.
<image>
<marquee>
XML 文档:
<?xml version="1.0" encoding="UTF-8"?>
<gameList>
<game id="2758" source="theGamesDB.net">
<path>./Zelda II - The Adventure of Link (USA).zip</path>
<name>Zelda II - The Adventure of Link (USA)</name>
<desc>Removed description for example</desc>
<image>./boxart/Zelda II - The Adventure of Link (USA).png</image>
<rating>0.68333</rating>
<releasedate>19880926T000000</releasedate>
<developer>Nintendo</developer>
<publisher>Nintendo</publisher>
<genre>Action</genre>
<players>1</players>
</game>
</gamelist>
我的代码在代码块的末尾附加一个选取框子元素.注意:这是一个循环遍历大量游戏的 for each 循环.为简单起见,我删除了创建选取框路径位置的代码并将其硬编码.
My code to append a marquee child element at the end of the code block. Note: This is a for each loop that cycles through a huge list of games. For simplicity I have taken out the code that creates the marquee path location and hard coded it in.
#Set the name of the game list
$InputXML = "gamelist.xml"
$OutputXML = "MODgamelist.xml"
#Load the existing document
[xml]$xml = Get-Content $InputXML
foreach($game in $xml.gamelist.game)
{
#Set Marquee Path
$marqueelPath = "./Zelda II - The Adventure of Link (USA).png"
#Add marquee node to game node parent
$marqueeElement = $xml.CreateElement("marquee")
$marqueeElement.InnerText = $marqueelPath
$game.AppendChild($marqueeElement)
}
#Output
$xml.save($OutputXML)
经过一番挖掘,我想出了这段代码,在图像之后插入新的子元素,但它出错了.如何正确选择图像节点然后在其后插入?
After some digging I have come up with this code to insert the new child element after image but it errors out. How do I correctly select the image node then insert after it?
$imageElement = $game.SelectSingleNode('//image')
$game.InsertAfter($marqueeElement, $imageElement)
错误:
Exception calling "InsertAfter" with "2" argument(s): "The reference node is not a child of this node."
At line:1 char:1
+ $game.InsertAfter($marqueeElement, $imageElement)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
提前致谢.
推荐答案
我正在阅读 MSDN 文档并找到了一个解决方案.https://msdn.microsoft.com/en-us/library/k44daxya.aspx
I was reading through the MSDN documentation and worked out a solution. https://msdn.microsoft.com/en-us/library/k44daxya.aspx
由于所有游戏节点块的布局都相同,我实际上可以通过数组编号调用参考节点.
With all the Game Node blocks being identical in layout, I can actually call the reference node by array number.
#Add marquee node to game node parent
$marqueeElement = $xml.CreateElement("marquee")
$marqueeElement.InnerText = $marqueelPath
$game.InsertAfter($marqueeElement, $game.ChildNodes[3])
这篇关于Powershell:在特定的当前同级元素之后添加新的 XML 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!