Powershell for non-N00bs: Creating XML the .NET Way


I’m dogmatic – if there is a ‘right’ way to do something, I want to do it (or at least learn how to do it) the ‘right’ way.

 

So often, I’ve seen


[xml]$xml = “<xml />”;


That’s one way to do it, but not the RIGHT way, dammit.  I want to use the constructor:


 

$xmlDocument = New-Object System.Xml.XmlDocument;
$ivaultsNode = $xmlDocument.CreateElement(“xml”);

 

Then we do stuff such as:


$fileNode = $xmlDocument.CreateElement(“file”);
$null = $fileNode.SetAttribute(“name”, $file);
$null = $xmlDocument.AppendChild($fileNode);


And finally we close with:


$null = $xmlDocument.AppendChild($ivaultsNode);


$xmlDocument;

Comments (0)