Powershell: Convert XML to String

前端 未结 3 1331
[愿得一人]
[愿得一人] 2021-01-01 09:33

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

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

相关标签:
3条回答
  • 2021-01-01 09:39

    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
    
    0 讨论(0)
  • 2021-01-01 09:48

    You are probably looking for OuterXml.

    $xml.OuterXml should give you what you want.

    0 讨论(0)
  • 2021-01-01 09:51

    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.

    0 讨论(0)
提交回复
热议问题