October 2017
Intermediate to advanced
440 pages
11h 47m
English
System.Xml.Linq can be used to create a document from scratch. For example:
using assembly System.Xml.Linq
using namespace System.Xml.Linq
$xDocument = [XDocument]::new(
[XDeclaration]::new('1.0', 'utf-8', 'yes'),
[XElement]::new('list', @(
[XAttribute]::new('type', 'numbers'),
[XElement]::new('name', 1),
[XElement]::new('name', 2),
[XElement]::new('name', 3)
))
)
Converting the xDocument object to a string shows the document without the declaration:
PS> $xDocument.ToString() <list type="numbers"> <name>1</name> <name>2</name> <name>3</name> </list>
The Save method may be used to write the document to a file:
$xDocument.Save("$pwd\test.xml")
Reviewing the document shows the declaration:
PS> Get-Content test.xml <?xml ...
Read now
Unlock full access