简体   繁体   中英

Convert XML Content to string or html format in powershell

I'm looking for a way to get xml content in string or in html:

I have an xml like:

<xml>
  <Students>
    <Name>Karan</Name>
    <Name>Rohan</Name>
    <Name>Jack</Name>
    <Name>Roy</Name>
  </Students>
</xml>

I wrote following code:

[xml]$Xml = Get-Content (student.xml)
$str = ConvertTo-Xml -InputObject $Xml.InnerXml -As Stream

But, it gives following string to $str and that too in a single line:

<?xml version="1.0"?> <Objects> <Object Type="System.String">&lt;Class&gt;&lt;Students&gt;&lt;Name&gt;Karan&lt;/Name&gt;&lt;Name&gt;Rohan&lt;/Name&gt;&lt;Name&gt;Jack&lt;/Name&gt;&lt;Name&gt;Roy&lt;/Name&gt;&lt;/Students&g
t;&lt;/Class&gt;</Object> </Objects>

But I want the string in following format including the whitespaces:

'<xml>
  <Students>
    <Name>Karan</Name>
    <Name>Rohan</Name>
    <Name>Jack</Name>
    <Name>Roy</Name>
  </Students>
</xml>'

也许

$str=gc C:\temp\test.xml |Out-String 

这样可以在保留格式的同时为您提供XML数据。

[String]$str = Get-Content -Path "student.xml" -Raw

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM