I'm learning PowerShell. I can load an xml file into a variable and manipulate it. I can then call the object's save method to save to disk. I expected there to be a way to output the resulting xml to screen, though. I can't seem to find one. Is there a way, other than outputting to file and then file-to-screen?
6 Answers
I couldn't get the Community Extensions to work and I don't really want to have to install something extra anyway. I have found another approach on a Microsoft blog -
function WriteXmlToScreen ([xml]$xml)
{
$StringWriter = New-Object System.IO.StringWriter;
$XmlWriter = New-Object System.Xml.XmlTextWriter $StringWriter;
$XmlWriter.Formatting = "indented";
$xml.WriteTo($XmlWriter);
$XmlWriter.Flush();
$StringWriter.Flush();
Write-Output $StringWriter.ToString();
}
$xml = [xml]'<root><so><user name="john">thats me</user><user name="jane">do you like her?</user></so></root>'
WriteXmlToScreen $xml
4 Comments
Write-Output makes standard function output so you can capture in a variable $formatted = WriteXmlToScreen $xml, or pipe somewhere WriteXmlToScreen $xml | Out-File $f[xml] datatype from the function declaration (ie. function WriteXmlToScreen ($xml)), otherwise PowerShell assumes that the variable is an XML document and displays the error: "Cannot convert value "System.Xml.XmlElement" to type "System.Xml.XmlDocument".Write-Output to something like Write-HostThe only way I know is using System.Xml properties like outerxml or innerxml. These properties should have code already indented as long as the source was.
3 Comments
$xmlObject.PreserveWhitespace = $true (where xmlObject is your variablename) before loading the xml data into $xmlObject in order to get the newlines and indents of the original.PreserveWhitespace. May be become relevant when outputting onto the screen.PreserveWhitespace the output to screen looses most returns and indents where save-to-file keeps them. Except for returns and indents that are part of inner text, it looks like.Oddly, with my test file, when saving to file one of the tabs got turned into two spaces, but the other tabs were fine.[System.Xml.Linq.XDocument]::Parse($Xml.OuterXml).ToString()
1 Comment
Linq assembly isn't available, load it beforehand with: [Reflection.Assembly]::LoadWithpartialName("System.Xml.Linq") | Out-NullLook at PSCX module. You will find Format-Xml cmdlet that does exactly that.
Example:
Import-Module pscx
$xml = [xml]'<root><so><user name="john">thats me</user><user name="jane">do you like her?</user></so></root>'
Format-Xml -InputObject $xml
will produce:
<root>
<so>
<user name="john">thats me</user>
<user name="jane">do you like her?</user>
</so>
</root>
For more info look at help format-xml -full
2 Comments
The cleanest solution I've found is using System.Xml.Linq.XDocument.Parse like this:
Write-Host ([System.Xml.Linq.XDocument]::Parse("$(Get-Content -path 'c:\myxml.xml' -Raw)"));
Comments
This is an old thread but I wanted to share my hackish answer. I needed to send the xml to php and I couldn't send anything else.
the answer I came up with was to save the file to disk and then run a get content on it. This echoes back the xml text and nothing else:
#hack alert.
#we need to echo out just the text of the XML back to PHP.
IF ("$env:TEMP\xml.xml") {Remove-Item "$env:TEMP\xml.xml"}
#$xmlDoc.Save("c:\temp\xml.xml")
$xmlDoc.Save("$env:TEMP\xml.xml")
get-content "$env:TEMP\xml.xml"
In my case I was sending it back to PHP and it worked perfectly