【问题标题】:Powershell XML Adding Child ElementPowershell XML 添加子元素
【发布时间】:2018-12-07 09:58:42
【问题描述】:

想要在 system.web 元素中添加另一个子元素。 到目前为止,我已经尝试了多种方法,但是,没有奏效。 我已经创建了这段代码,我不确定它是否接近。

到目前为止的代码:

#setting variables
$path = "H:\PSTesting\Logs\ExerciseXml.xml"
$xml = Get-Content $path

#Creating element
$child = $xml.CreateElement("Test")

#setting attributes
$child.SetAttribute('Testing','hey = "something"')

#adding attributes to the location
$xml.'system.web'.AppendChild($child)

#save file
$xml.Save($path)

下面是我需要更改的 XML。 当前:

<?xml version="1.0" encoding="UTF-8"?>
-<configuration>
    -<system.web>
        <authentication mode="None"/>
        <compilation targetFramework="4.5.1" debug="false"/>
        <httpRuntime targetFramework="4.5.1"/>
    </system.web>
</configuration>

以下是运行代码的预期结果。

<?xml version="1.0" encoding="UTF-8"?>
-<configuration>
    -<system.web>
        <authentication mode="None"/>
        <compilation targetFramework="4.5.1" debug="false"/>
        <httpRuntime targetFramework="4.5.1"/>
        <Testing Hey = 'something'>
    </system.web>
</configuration>

任何帮助将不胜感激。 提前致谢!

【问题讨论】:

    标签: xml powershell scripting


    【解决方案1】:

    你已经很接近了,只是一些变化:

    #setting variables
    $path = "H:\PSTesting\Logs\ExerciseXml.xml"
    [xml]$xml = Get-Content $path
    
    #Creating element
    $child = $xml.CreateElement("Testing")
    
    #setting attributes
    $child.SetAttribute('hey','something')
    
    #adding attributes to the location
    $xml.configuration.'system.web'.AppendChild($child)
    
    #save file
    $xml.Save($path)
    

    顺便说一句,您可能希望从示例中删除 -,因为它们具有误导性(xml 实际上并不包含它们,并且它们仅在从 IE 等程序中打开时可见)

    编辑:正如Rohin Sidharth 所提到的,最好指定类型(尽管只要文件格式正确,PowerShell 就会自动检测到它)。

    Edit2:澄清问题所在:

    $child = $xml.CreateElement("Test")
    

    这将根据您想要的输出创建名为 Test 的元素,而您想要 Testing。

    $child.SetAttribute('Testing','hey = "something"')
    

    这将创建属性Testing,其值为hey = "something"

    $xml.'system.web'.AppendChild($child)
    

    这不起作用,因为正确的路径是 $xml.configuration.'system.web' 而不是 $xml.'system.web'。

    【讨论】:

    • 该死的,我是如此接近。泰寻求帮助。
    【解决方案2】:

    我不相信 Robdy 的回答会起作用,因为它没有解决真正的问题,即 Get-Content 命令将您的 xml 作为文本文件读取。因此,脚本中使用的 xml 属性和方法将不起作用。

    但是,答案非常简单:将$xml 类型转换为[xml]

    #setting variables
    $path = "H:\PSTesting\Logs\ExerciseXml.xml"
    [xml]$xml = Get-Content $path                 #typecasting to xml here
    
    #Creating element
    $child = $xml.CreateElement("Test")
    
    #setting attributes
    $child.SetAttribute('Testing','hey = "something"')
    
    #adding attributes to the location
    $xml.'system.web'.AppendChild($child)
    
    #save file
    $xml.Save($path)
    

    就像 Robdy 提到的那样,- 具有误导性。那不是xml格式。

    【讨论】:

    • 很好——这是指定类型的好习惯。但是,在这个特定示例中,它将由 PowerShell 自动完成(只要文件格式正确)
    • 我认为它会,但当我尝试并且我有 PS 5.1 时,它对我不起作用。除了类型转换让我不依赖于 powershell 的摆布。
    • 为“-”道歉。这通常不存在,它只是一个折叠的 xml。这样做是否简化了我的数据。 Ty 为现场。
    猜你喜欢
    • 2017-10-22
    • 2013-10-15
    • 1970-01-01
    • 2012-06-14
    • 2017-03-11
    • 1970-01-01
    • 2013-07-19
    • 2019-01-10
    • 1970-01-01
    相关资源
    最近更新 更多