gpt4 book ai didi

xml - 如何使用 Powershell 在 xml 中附加新属性?

转载 作者:数据小太阳 更新时间:2023-10-29 02:55:41 24 4
gpt4 key购买 nike

我有这样的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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com