我有这样的 xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<DATA>
<Platform>
<D1>8536</D1>
</Platform>
<Timestamp>
<Create>Friday, August 23, 2019 4:34:18 PM</Create>
</Timestamp>
</DATA>
我想在<Timestamp>
中添加一个元素。
我的期望是这样的
<?xml version="1.0" encoding="UTF-8"?>
<DATA>
<Platform>
<D1>8536</D1>
</Platform>
<Timestamp>
<Create>Friday, August 23, 2019 4:34:18 PM</Create>
<Info>Started</Info>
</Timestamp>
</DATA>
这是我迄今为止的尝试:
$fileName = "D:\file.xml"
$xmlDoc = [System.Xml.XmlDocument](Get-Content $fileName)
$newXmlEmployeeElement = $xmlDoc.CreateElement("Info")
$newXmlEmployee = $xmlDoc.DATA.Timestamp.AppendChild($newXmlEmployeeElement)
$xmlDoc.Save($fileName)
任何人都可以提供帮助,非常感激。谢谢
最佳答案
您的代码中唯一缺少的就是为新的 <Info>
创建内容元素:
$newXmlEmployeeElement.InnerText = 'Started'
关于xml - 如何使用 Powershell 在 xml 中附加新属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57622785/