如何在PowerShell中添加XML元素
发布时间:2020-12-16 08:04:24 所属栏目:百科 来源:网络整理
导读:我正在为这个xml创建一个子XML元素: ?xml version="1.0" encoding="utf-8"?configuration/configuration 我使用这个PowerShell脚本: [xml] $doc = Get-Content($filePath)$child = $doc.CreateElement("newElement")$doc.configuration.AppendChild($child
我正在为这个xml创建一个子XML元素:
<?xml version="1.0" encoding="utf-8"?> <configuration> </configuration> 我使用这个PowerShell脚本: [xml] $doc = Get-Content($filePath) $child = $doc.CreateElement("newElement") $doc.configuration.AppendChild($child) 我有一个错误:
如果您使用点符号来导航XML文件(例如$ doc.configuration),则Powershell会尝试对其返回的内容有所了解。
>如果目标元素为空或仅包含单个文本节点,则PS将返回一个String。 在您的情况下,您很容易将节点附加到文档元素中: [xml] $doc = Get-Content($filePath) $child = $doc.CreateElement("newElement") $doc.DocumentElement.AppendChild($child) 但是您可以使用$ doc.SelectNodes()或$ doc.SelectSingleNode()来浏览XML文档,并始终返回一个节点/节点列表。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |