Xml Powershell如何仅替换给定数量的匹配项?

Xml Powershell如何仅替换给定数量的匹配项?,xml,powershell,batch-file,windows-10,Xml,Powershell,Batch File,Windows 10,我正在尝试在Windows 10上编写批处理脚本 powershell -Command "(gc example.xml) -replace '(?s)<version>.*?</version>', '<version>2.0.0</version>' | Out-File example.xml" 在XML文件中,我有多个“版本”标记。使用powershell,我只想替换前两个 powershell -Command "(gc exampl

我正在尝试在Windows 10上编写批处理脚本

powershell -Command "(gc example.xml) -replace '(?s)<version>.*?</version>', '<version>2.0.0</version>' | Out-File example.xml"
在XML文件中,我有多个“版本”标记。使用powershell,我只想替换前两个

powershell -Command "(gc example.xml) -replace '(?s)<version>.*?</version>', '<version>2.0.0</version>' | Out-File example.xml"
powershell-Command”(gc example.xml)-替换“(?s)。*?”,“2.0.0”| Out File example.xml”

但该代码将替换所有这些代码。我怎样才能只替换其中的两个呢?

我通常同意应该使用xml工具编辑xml文件,
powershell -Command "(gc example.xml) -replace '(?s)<version>.*?</version>', '<version>2.0.0</version>' | Out-File example.xml"
我认为不可能把它塞进一行程序中,但是这个
example.xml

powershell -Command "(gc example.xml) -replace '(?s)<version>.*?</version>', '<version>2.0.0</version>' | Out-File example.xml"
> type .\example.xml
<version>1.0.1</version>
<version>1.0.2</version>
<version>1.0.3</version>
<version>1.0.4</version>

for loops
是您正在寻找的神奇单词。您能给我一个您的
example.xml
文件的santizied版本吗。当我说sanitized时,我的意思是替换任何IP地址、计算机名、域名、帐户名等。通常的答案是,“不要使用正则表达式更新XML文件。”loops+Powershell具有读取和写入XML文件的本机功能。这就是我想要的。非常感谢。