小编J. *_*Sam的帖子

如何使用 insertAfter() 在 XML 中的特定元素之后插入元素

我的文件 .xml 看起来像这样

<?xml version="1.0" encoding="UTF-8"?>
<layoutMaster xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="..\schema\m.xsd">
  <deviceFamily>
    <deviceFamilyUniqueID>DeviceFamilyWayside_2</deviceFamilyUniqueID>
    <name>DeviceFamilyWayside_2</name>
    <device>SAMSUNG</device>
    <device>SONY</device>
    <deviceFamilyOptions>
      <name>false</name>
    </deviceFamilyOptions>
  </deviceFamily>
  <deviceFamily>
    <deviceFamilyUniqueID>DeviceFamilyWayside_4</deviceFamilyUniqueID>
    <name>DeviceFamilyWayside_4</name>
    <device>IPHONE</device>
    <device>MAC</device>
    <deviceFamilyOptions>
      <name>false</name>
    </deviceFamilyOptions>
  </deviceFamily>
</layoutMaster>
Run Code Online (Sandbox Code Playgroud)

我想在deviceFamily[deviceFamilyUniqueID ='DeviceFamilyWayside_2']最后一个设备之后添加一个新设备:

所以我在 PowerShell 中尝试了以下代码:

$Path = ".\config.xml"
[xml]$xmlFile = Get-Content -Path $Path

$xmlNewElementDevice = $xmlFile.CreateElement("device")
$xmlNewElementDevice.AppendChild($xmlFile.CreateTextNode("ALCATEL"))

$xmlTargetDeviceWayside = $xmlFile.SelectSingleNode('//layoutMaster/deviceFamily[deviceFamilyUniqueID = "DeviceFamilyWayside_2"]')
$xmlTargetDeviceWayside.InsertAfter($xmlNewElementDevice,$xmlTargetDeviceWayside.name)
Run Code Online (Sandbox Code Playgroud)

但我得到了这个错误:

无法将参数“1”(值“DeviceFamilyWayside_2”)从“InsertAfter”转换为类型“System.Xml.XmlNode”:无法将“DeviceFamilyWayside_2”值从“System.String”转换为类型“System.Xml.XmlNode” ”。”

我想要得到的最终结果是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<layoutMaster xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="..\schema\m.xsd">
  <deviceFamily>
    <deviceFamilyUniqueID>DeviceFamilyWayside_2</deviceFamilyUniqueID>
    <name>DeviceFamilyWayside_2</name>
    <device>SAMSUNG</device>
  <device>SONY</device>
    <device>ALCATEL</device>    <--------------- here
    <deviceFamilyOptions>
      <name>false</name>
    </deviceFamilyOptions>
  </deviceFamily>
  <deviceFamily>
    <deviceFamilyUniqueID>DeviceFamilyWayside_4</deviceFamilyUniqueID>
    <name>DeviceFamilyWayside_4</name>
    <device>IPHONE</device>
  <device>MAC</device> …
Run Code Online (Sandbox Code Playgroud)

xml powershell parsing element insertafter

5
推荐指数
1
解决办法
4254
查看次数

如何在解析的 JSON 数组中添加对象?

我有这个文件 .json:

{
  "topologyTypes": {
    "stations": {
      "instances": [
        {
          "@name": "value1",
          "@address": "value2"
        },
        {
          "@name": "value3",
          "@address": "value4"
        }         
      ]
    }
  },
  "agg": {},
  "inter": {}
}
Run Code Online (Sandbox Code Playgroud)

我想使用 PowerShell 在拓扑类型.stations.instances 中添加这样的对象

{
    "@name": "value4",
    "@adress": "value5"
}
Run Code Online (Sandbox Code Playgroud)

所以我在 PowerShell 中尝试了以下代码,但它不起作用:

$path = "./data.json"
$jsonFile = Get-Content $path -Raw | ConvertFrom-Json

$jsonContent = @"
    {
        "@name": "value4",
        "@adress": "value5"
    }
"@

$jsonFile.topologyTypes.stations.instances |
    Add-Content -Value (ConvertFrom-Json $jsonContent)
Run Code Online (Sandbox Code Playgroud)

我想要得到的期望输出是这样的:

{
  "topologyTypes": {
    "stations": {
      "instances": [
        {
          "@name": "value1",
          "@address": …
Run Code Online (Sandbox Code Playgroud)

arrays powershell json object addition

1
推荐指数
1
解决办法
3155
查看次数

标签 统计

powershell ×2

addition ×1

arrays ×1

element ×1

insertafter ×1

json ×1

object ×1

parsing ×1

xml ×1