Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been working on an xml structure and Im having issues adding to a specifc part of the XML file and need help. My goal is to add new category under the Categories node. below is my current code.

$Path = "C:\Users\shuppz\Desktop\XML\Helpbox.xml"

$xmlDoc = [System.Xml.XmlDocument](Get-Content $Path)
		
#Create New Category
$newCat = $xmlDoc.AdminLauncher.Categories.AppendChild($xmlDoc.CreateElement("Category"))
		
#Set Attributes
$newCat.SetAttribute("Selected", "False")
$newCat.SetAttribute("Name", $Name)

#Save File
$xmlDoc.Save($Path)


My XML:

XML
<?xml version="1.0"?>
<!--XML For Admin Launcher-->
<AdminLauncher>
  <Categories />
  <Task Name="GPMC" Category="Tools">
    <Path>C:\Windows</Path>
    <Arg>/offerra</Arg>
  </Task>
</AdminLauncher>




When I run it I get the below errors:

Method invocation failed because [System.String] doesn't contain a method named 'AppendChild'.
At C:\Users\shuppz\AppData\Local\Temp\5564e900-f0cf-4878-8c39-9fc3aff6ed8e.ps1:6 char:55
+ $newCat = $xmlDoc.AdminLauncher.Categories.AppendChild <<<< ($xmlDoc.CreateElement("Category"))
    + CategoryInfo          : InvalidOperation: (AppendChild:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
 
You cannot call a method on a null-valued expression.
At C:\Users\shuppz\AppData\Local\Temp\5564e900-f0cf-4878-8c39-9fc3aff6ed8e.ps1:9 char:21
+ $newCat.SetAttribute <<<< ("Selected", "False")
    + CategoryInfo          : InvalidOperation: (SetAttribute:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
You cannot call a method on a null-valued expression.
At C:\Users\shuppz\AppData\Local\Temp\5564e900-f0cf-4878-8c39-9fc3aff6ed8e.ps1:10 char:21
+ $newCat.SetAttribute <<<< ("Name", $Name)
    + CategoryInfo          : InvalidOperation: (SetAttribute:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
Posted
Updated 9-Aug-18 3:56am
v2
Comments
Zoltán Zörgő 16-Nov-13 15:01pm    
Could you post a sample XML file?
RedDk 16-Nov-13 15:14pm    
A quick check using VisualStudio 2010 "Object Browser" -> System.Xml.XmlDocument reveals no clue. Checking -> System.String also reveals no clue. From where comes this script?
RedDk 16-Nov-13 15:39pm    
Also, there IS System.XMLNode and System.XMLPAttribute both of which DO have a method to "AppendChild", as you say.
Sergey Alexandrovich Kryukov 17-Nov-13 1:24am    
If you really want to append to XML, not insert something, this is impossible in principle. It would also give the file which is not well-formed XML. It's that just your formulation in the question title is wrong. You need to append child.
—SA

1 solution

Could you post a sample XML file?
I don't think you can address the node like this: ".AdminLauncher.Categories", you have to select it to be able to add a child node.
Something like this:
$newCat = $xmlDoc.CreateElement("Category")
$xmlDoc.SelectSingleNode("//AdminLauncher/Categories").AppendChild($newCat)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900