Powershell: Convert XML to String

╄→гoц情女王★ 提交于 2019-12-04 15:09:19

问题


I am searching for a way to convert a XML-Object to string.

Is there a way like $xml.toString() in Powershell?


回答1:


You are probably looking for OuterXml.

$xml.OuterXml should give you what you want.




回答2:


How are you creating the XML object?

Typically, if you want an XML string from an object, you'd use:

$object | ConvertTo-Xml -As String



回答3:


Try this:

[string[]]$text = $doc.OuterXml #or use Get-Content to read an XML File
$data = New-Object System.Collections.ArrayList
[void] $data.Add($text -join "`n")
$tmpDoc = New-Object System.Xml.XmlDataDocument
$tmpDoc.LoadXml($data -join "`n")
$sw = New-Object System.IO.StringWriter
$writer = New-Object System.Xml.XmlTextWriter($sw)
$writer.Formatting = [System.Xml.Formatting]::Indented
$tmpDoc.WriteContentTo($writer)
$sw.ToString()

I used this script to write my generated XML into a TextBox in Windows Forms.



来源:https://stackoverflow.com/questions/15410455/powershell-convert-xml-to-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!