Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell XML添加子元素_Xml_Powershell_Scripting - Fatal编程技术网

Powershell XML添加子元素

Powershell XML添加子元素,xml,powershell,scripting,Xml,Powershell,Scripting,希望在system.web元素中添加另一个子元素。 到目前为止,我已经尝试了很多方法,但是,这些方法都是有效的。 我已经创建了这段代码,我不确定它是否接近 迄今为止的代码: #setting variables $path = "H:\PSTesting\Logs\ExerciseXml.xml" $xml = Get-Content $path #Creating element $child = $xml.CreateElement("Test") #setting attributes

希望在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>

-
-
任何帮助都将不胜感激。
提前谢谢

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

#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之类的程序中打开时才可见)

编辑:如所述,最好指定类型(尽管只要文件格式正确,PowerShell将自动检测)

Edit2:要澄清错误:

$child = $xml.CreateElement("Test")
这将创建名为
Test
的元素,而您需要基于所需的输出进行
Testing

$child.SetAttribute('Testing','hey = "something"')
这将使用值创建属性
测试


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

,我不相信Robdy的答案会起作用,因为它没有解决真正的问题,即将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格式。

Nice catch-指定类型是一个很好的实践。但是,在这个特定的示例中,它将由PowerShell自动完成(只要文件格式正确),我想它会自动完成,但在我尝试使用PS 5.1时,它对我不起作用。此外,打字让我不必依赖powershell的仁慈。为“-”道歉。这通常不存在,它只是一个折叠的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)