February 2019
Intermediate to advanced
626 pages
15h 51m
English
System.Xml.Linq can be used to create a document from scratch, for example:
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 into 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 version="1.0" encoding="utf-8" ...
Read now
Unlock full access