xml - 用于更新 XML 文件的 Powershell 脚本
问题描述
有什么方法可以使用 PowerShell 更新我的 XML 文件:
我当前的文件格式:
<?xml version="1.0"?>
<Configuration>
<SPaths>
<path>Share</path>
</SPaths>
我的输出应该是:
<?xml version="1.0"?>
<Configuration>
<SPaths>
<path>My Share</path>
<path>My Share/xyz</path>
<path>My Share/abc</path>
</SPaths>
我正在使用以下命令
$myXML = [xml](Get-Content 'C:\MyPath.config')
$myXML.Configuration.SPaths.path = "My Share"
$myXML.Save("C:\MyPath.config")
我需要再添加 2 行代码,非常感谢任何帮助,谢谢。
解决方案
使用下面给出的命令,我可以在我的 XML 文件中添加新行。谢谢丹尼尔
$myXML.Configuration.SPaths.path = "My Share"
$xmlElt = $myXML.CreateElement("path")
$xmlText = $myXML.CreateTextNode("My Share/xyz")
$xmlElt.AppendChild($xmlText)
$myXML.Configuration.SPaths.InsertBefore($xmlElt, $myXML.Configuration.SPaths.legacyUnhandledExceptionPolicy)
$xmlElt1 = $myXML.CreateElement("path")
$xmlText1 = $myXML.CreateTextNode("My Share/abc")
$xmlElt1.AppendChild($xmlText1)
$myXML.Configuration.SPaths.InsertBefore($xmlElt1, $myXML.Configuration.SPaths.legacyUnhandledExceptionPolicy)
$myXML.Save("C:\MyPath.config")```
推荐阅读
- javascript - 反应:useEffect “悖论”与 eventHandler 道具
- angular - 如何使用组件而不是 html 作为 Angular CLI 中另一个组件的插槽的内容?
- python - 在没有赋值的情况下在python中使用行程运算符可以吗?
- string - 为什么我的 thymeleaf #strings 不起作用?
- avr - 如何修复错误:在 atmega328p 中,“asm”之前的预期标识符或“(”
- android - Android 工具链 - 为 Android 设备开发
- arrays - 从 mongodb 中的整数文本文件写入以进行排序和查找距离
- javascript - 如果主链接在 HTML 中不可用,如何连接一个主链接和另一个链接?
- c# - 从 Ajax 函数 Web 方法调用 BOT 监听 URL
- sql-server - 使用 Python 将数据框插入 SQL Server 时出错