1

Have a very complex task for Powershell on remote work for me. It's hard to explain but I will try. I have XML file with complex structure (it's a file with settings for Visual Studio):

<UserSettings>
<ToolsOptions>
<ToolsOptionsCategory RegisteredName="Environment" name="Environment">
</ToolsOptionsCategory>
</ToolsOptions>
</UserSettings>

The task is add to section "ToolsOptionsCategory" following sub-node:

<ToolsOptionsSubCategory PackageName="VS Setup Composition" RegisteredName="ProductUpdates" name="ProductUpdates">
<PropertyValue name="IsBackground">true</PropertyValue>
</ToolsOptionsSubCategory>

I tried to AppendChild and CreateElement, but I don't have an idea how to add such attributes as "PackageName" and "RegistereName". This subnode is absent from the target file and I need to add it. I would be great if you can help me and show me the way.

Thank you in advance! Be health and stay at home :)

2 Answers 2

2

For demo, I'm using a Here-String

[xml]$xml = @"
<UserSettings>
<ToolsOptions>
<ToolsOptionsCategory RegisteredName="Environment" name="Environment">
</ToolsOptionsCategory>
</ToolsOptions>
</UserSettings>
"@

In real life, you would load it from file with [xml]$xml = Get-Content -Path 'D:\config.xml'

$node = $xml.UserSettings.ToolsOptions.ToolsOptionsCategory
# update the attributes for this node
$node.SetAttribute("PackageName", "VS Setup Composition")
$node.SetAttribute("RegisteredName", "ProductUpdates")
$node.SetAttribute("name", "ProductUpdates")
# create the new sub node (XmlElement)
$newNode = $xml.CreateElement("PropertyValue")
$newNode.SetAttribute("name", "IsBackground")
$newNode.InnerText = "true"
# add this new subnode to the 'ToolsOptionsCategory' node
$node.AppendChild($newNode)

$xml.Save('D:\newConfig.xml')

Result:

<UserSettings>
  <ToolsOptions>
    <ToolsOptionsCategory RegisteredName="ProductUpdates" name="ProductUpdates" PackageName="VS Setup Composition">
      <PropertyValue name="IsBackground">true</PropertyValue>
    </ToolsOptionsCategory>
  </ToolsOptions>
</UserSettings>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! Great! I've checked your answer and have small changes regarding this current situation. Your answer is working!
@Tube Thanks for the nice feedback. Stay safe!
This is good. I included a ToolsOptionsSubCategory sub node as well from the question. I guess it doesn't really matter if its not needed.
1

Here's another way to do is as well. I've added comments to explain the approach.

# Create XML object to load data into
$xml = New-Object -TypeName System.Xml.XmlDocument

# Load in XML file
$xml.Load("test.xml")

# Get root tools node to add children
$toolsRootNode = $xml.UserSettings.ToolsOptions.ToolsOptionsCategory

# Store attributes in a hashtable
$toolsAttributes = [ordered]@{
    PackageName="Setup Composition"
    RegisteredName="ProductUpdates"
    name="ProductUpdates"
}

# Set each attribute from hashtable
$toolsSubNode = $xml.CreateElement("ToolsOptionsSubCategory")
foreach ($kvp in $toolsAttributes.GetEnumerator())
{
    $toolsSubNode.SetAttribute($kvp.Key, $kvp.Value)
}

# Create property sub node
$propertyNode = $xml.CreateElement("PropertyValue")
$propertyNode.SetAttribute("name", "IsBackground")
$propertyNode.InnerText = "true"

# Append property node to tools sub node
$toolsSubNode.AppendChild($propertyNode)

# Finally add tools sub node to root node
$toolsRootNode.AppendChild($toolsSubNode)

# Save to new output XML file
$xml.Save("output.xml")

output.xml

<UserSettings>
  <ToolsOptions>
    <ToolsOptionsCategory RegisteredName="Environment" name="Environment">
      <ToolsOptionsSubCategory PackageName="Setup Composition" RegisteredName="ProductUpdates" name="ProductUpdates">
        <PropertyValue name="IsBackground">true</PropertyValue>
      </ToolsOptionsSubCategory>
    </ToolsOptionsCategory>
  </ToolsOptions>
</UserSettings>

2 Comments

Thank you @RoadRunner, I've checked your solution and it is also working :) Comments are very helpful because now I really know how to deal with XML and Powershell.
@Tube No worries. Glad it was useful to you.

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.