Good evening,
I have been working with XML and I have a problem I don't know how to to solve.
This is the original XML file:
<?xmlversion="1.0"?><FavoritesFilexmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Groups><Groupid="36eb4236-5eeb-43e2-89b4-0115c77f4a23"><Parent>a83ea076-bf80-414b-9218-54e2e062328e</Parent><Name>Alfred</Name></Group></Groups></FavoritesFile>
This is my script so far:
$Current_Folder=Split-Path-parent$MyInvocation.MyCommand.Definition$XML_File=$Current_Folder+"\Favorites.xml"[xml]$XMLObject=Get-Content$XML_File-EncodingUTF8$xmlElt=$XMLObject.CreateElement('Group')$xdNS=$XMLObject.DocumentElement.NamespaceURI$newXmlGroup=$XMLObject.CreateElement('Group',$xdNS)$newXmlGroupElement_Parent=$newXmlGroup.AppendChild($XMLObject.CreateElement("Parent",$xdNS));$newXmlGroupElement_Parent=$newXmlGroup.AppendChild($XMLObject.CreateTextNode("00000000-0000-0000-0000-000000000000"));$newXmlGroupElement_Name=$newXmlGroup.AppendChild($XMLObject.CreateElement("Name",$xdNS));$newXmlGroupElement_Name=$newXmlGroup.AppendChild($XMLObject.CreateTextNode("Test_Name"));$XMLObject.FavoritesFile.Groups.AppendChild($newXmlGroup)$XMLObject.Save($XML_File);
When I run the previous script, this is what i've got:
<?xmlversion="1.0"?><FavoritesFilexmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"FileVerion="2.0"xmlns="http://Terminals.codeplex.com"><Groups><Groupid="36eb4236-5eeb-43e2-89b4-0115c77f4a23"><Parent>a83ea076-bf80-414b-9218-54e2e062328e</Parent><Name>Alfred</Name></Group><Group><Parent/>00000000-0000-0000-0000-000000000000<Name/>Test_Name</Group></Groups></FavoritesFile>
I want my script to Add node to my XML, with its closing elements.
This is what I want:
<?xmlversion="1.0"?><FavoritesFilexmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Groups><Groupid="36eb4236-5eeb-43e2-89b4-0115c77f4a23"><Parent>a83ea076-bf80-414b-9218-54e2e062328e</Parent><Name>Alfred</Name></Group><Groupid="36eb4236-5eeb-43e2-89b4-0115c77f4a23"><Parent>00000000-0000-0000-0000-000000000000</Parent><Name>Test_Name</Name></Group></Groups></FavoritesFile>
My XML is much bigger and i have a lot of nodes and some other different nodes.
Any ideas would be apprecciated.
Kind Regards,
Antonio