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

标签 xml powershell

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

相关文章:

android - 如何在我的应用程序中添加 android 菜单溢出图标

python - 将条目添加到现有 XMLFile Python

PowerShell - 使用重复行扩展数组中的数组列

powershell - 自动数组包装/展开的规则是什么?

powershell - 在powershell中获取巧克力刚刚安装的软件包的安装路径

shell - Windows Powershell 中的 Unix 命令等效项

c# - 如何从 XML 文件中获取所有文本节点

java - 是否有任何流行的库可以在不使用绑定(bind)的情况下处理用 Java 编写 XSD?

Android UI重力="right"不起作用

powershell - 仅替换文件中的文本特定次数