xml - 方法调用失败,因为 [System.String] 不包含名为“AppendChild”的方法
问题描述
突然,这段代码不再工作了。我正在尝试用谷歌搜索,但似乎找不到任何解决方案来解决这个问题。
我不能只做
[xml]($deploymentXml.configuration.services).AppendChild($newService)
或者
$deploymentServicesNode = [xml]$deploymentXml.configuration.services
$deploymentServicesNode.AppendChild($newService)
有谁知道发生了什么以及为什么这不再起作用?
部署.xml
<?xml version="1.0" encoding="utf-8"?>
<configuration description="Services deployment server">
<services>
</services>
</configuration>
方法调用失败,因为 [System.String] 不包含名为“AppendChild”的方法。在 F:\USB\host.ps1:153 char:9 + $deploymentXml.configuration.services.AppendChild($service ... + ~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [ ],RuntimeException + FullyQualifiedErrorId:MethodNotFound
host.ps1部分
#Location of the file
$deploymentFile = $PSScriptRoot + "\USB\Configuration\Deployment.xml";
$deploymentXml = [xml](Get-Content $deploymentFile);
# check if we got a file, if not do nothing.
if ($deploymentXml) {
Write-Verbose "deployment xml: $deploymentFile"
foreach($service in $Services) {
# Create the new service
$newService = $deploymentXml.CreateElement("service");
$deploymentXml.configuration.services.AppendChild($newService)
Write-Verbose "Adding service: $service"
$newService.SetAttribute("name", $service);
}
$deploymentXml.Save($deploymentFile);
}
解决方案
在获取您的 XML 内容之前,请尝试添加以下内容:
$deploymentFile = $PSScriptRoot + "\USB\Configuration\Deployment.xml";
[System.Xml.XmlDocument]$deploymentXml = New-Object System.Xml.XmlDocument
$deploymentXml = [xml](Get-Content $deploymentFile);
我遇到了一些在某些环境中未设置类型的问题,即 SQL 作业 PowerShell 步骤。
推荐阅读
- mongodb - 如何从 MongoDB 聚合和获取记录?
- javascript - 将数字四舍五入到Javascript中数组中最接近的最高数字
- javascript - 如何在 JavaScript 中将 UTC 日期和时间转换为 EST 日期和时间?
- python - 如何以行格式打印数据帧差异结果
- c# - 如何在 WPF 的功能区中访问 DataTemplate 中的文本块?
- python - 使用 pyodbc 从 SQL Server 中提取数据
- python - 图形化 Python 密码生成器:tkinter
- json - 节点名称中 JSON Xpath 的通配符
- python - Python权限被拒绝linux
- python - 字符串相乘-Leetcode using Karatsuba algorithm with Python