1

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!

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Nice catch - that's a good practice to specify type. However, in this particular example it'll be done automatically by PowerShell (as long as the file format is correct)
I figured it would but didn't work for me when i tried and i have PS 5.1. Besides typecasting lets me to not depend on the mercy of powershell.
Apologies for the '-'. This isn't not usually there, its just a collapsed xml. Did it so simplify my data. Ty for the spot.
1

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'.

1 Comment

Damn I was so close. Ty for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.