简体   繁体   中英

Powershell XML Adding Child Element

Wanting to add another child element in system.web element. So far I have tried a number of ways, however, non have worked. I have created this code, which I'm not sure is close or not.

Code so far:

#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)

Below is my XML which needs to be changed. Current:

<?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>

Below is the desired outcome from running the code.

<?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>

Any help would be appreciated. Thanks in advance!

I do not believe that Robdy's answer would work because it does not address the real problem which is the Get-Content command which reads your xml as a text file. Hence, the xml properties and methods used in the script would not work.

However, the answer is really simple: TypeCasting the $xml to [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)

Like Robdy mentioned, the - is misleading. That is not xml format.

You're quite close, just few changes:

#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)

By the way, you might want to remove - from your example as they're misleading ( xml doesn't actually contain them and they're only visible while opening it from programs like IE)

Edit : as mentioned by Rohin Sidharth , as a best practice would be good to specify the type (although PowerShell will detect it automatically as long as the file format is correct).

Edit2 : to clarify what was wrong:

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

This would create element named Test while you wanted Testing based on your desired output.

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

This would create attribute Testing with value hey = "something"

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

This won't work as correct path is $xml.configuration.'system.web' instead of $xml.'system.web' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM