使用powershell编辑多个XML文件
如何使用 powershell 从指定目录获取多个 XML 文件的列表,并为每个文件在第二个根节点下添加一个元素?
例子: 我想在第一个
元素中添加
:
<People>
<Names>
<FirstName>someFirstName</FirstName>
</Names>
<Names>
<FirstName>myFirstName</FirstName>
<Address>SomeAddress</Address>
</Names>
</People>
将变为:
<People>
<Names>
<LastName>SomeName</LastName>
<FirstName>someFirstName</FirstName>
</Names>
<Names>
<FirstName>myFirstName</FirstName>
<Address>SomeAddress</Address>
</Names>
</People>
How can I get a list of multiple XML files from a specified directory and for each file add an element under the second root node using powershell?
Example:
I want to add <LastName>SomeName</LastName>
within the FIRST <Names>
element:
<People>
<Names>
<FirstName>someFirstName</FirstName>
</Names>
<Names>
<FirstName>myFirstName</FirstName>
<Address>SomeAddress</Address>
</Names>
</People>
Will become:
<People>
<Names>
<LastName>SomeName</LastName>
<FirstName>someFirstName</FirstName>
</Names>
<Names>
<FirstName>myFirstName</FirstName>
<Address>SomeAddress</Address>
</Names>
</People>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
CreateElement
和AppendChild
方法来完成此操作。如果您运行 PowerShell V2,则不需要使用属性
PsBase
:有当然还有其他方法,但这是一种非常简单的方法。
如果节点在 xml 中更深,您可以像这样使用 Xpath(两者都找到第一个
Names
节点):You can do it using
CreateElement
andAppendChild
methodIn case that you run PowerShell V2, you don't need to use property
PsBase
:There are for sure other ways, but this is one is quite easy.
In case that the node would be deeper in xml, you might use Xpath like this (both find first
Names
node):