Powershell拆分xml节点并添加到同一文件中

时间:2016-05-27 09:57:04

标签: xml powershell

我正在解析xml文件,特别是节点我必须首先将其拆分,然后在其中添加操作,然后在其上面插入新节点。

我的XML文件:

<Project Default="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="1.0">
<Items>
    <Resource Include="File\Scripts\Main.xml" />
    <Resource Include="File\Scripts\File101_102.xml" />
</Items>
</project>

Powershell的:

[xml]$xdoc = Get-Content $path
$NodeInsertAfter=$xdoc.project.Items.Resource[0]
$StringtoSplit=$xdoc.project.Items.Resource[1]
$a=$StringtoSplit.Include.ToString().split('\')
$a=$StringtoSplit.Include.ToString().split('\')
$a

我已经达到读取节点但是如何使用split函数来增加文件号,即File102_103.xml

我的输出必须是:

<Project Default="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="1.0">
<Items>
    <Resource Include="File\Scripts\Main.xml" />
    <Resource Include="File\Scripts\File102_103.xml" />
    <Resource Include="File\Scripts\File101_102.xml" />
</Items>

1 个答案:

答案 0 :(得分:0)

您可以使用regex提取两个数字:

$fileNumberRegex = '(\d+)_(\d+)'
$match = [regex]::Match($StringtoSplit.Include, $fileNumberRegex)
$StringtoSplit.Include -replace $fileNumberRegex, ('{0}_{1}' -f (1+$match.Groups[1].Value), (1+$match.Groups[2].Value))