xml Powershell,输出xml到屏幕

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6142053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 14:46:47  来源:igfitidea点击:

Powershell, output xml to screen

xmlpowershell

提问by Vimes

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?

我正在学习 PowerShell。我可以将一个 xml 文件加载到一个变量中并对其进行操作。然后我可以调用对象的 save 方法来保存到磁盘。不过,我希望有一种方法可以将结果 xml 输出到屏幕上。我似乎找不到一个。除了输出到文件然后文件到屏幕之外,还有其他方法吗?

采纳答案by stej

Look at PSCX module. You will find Format-Xmlcmdlet that does exactly that.

看看PSCX 模块。您会发现Format-Xmlcmdlet 正是这样做的。

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

有关更多信息,请查看 help format-xml -full

回答by samaspin

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

回答by Emiliano Poggi

The only way I know is using System.Xmlproperties like outerxmlor innerxml. These properties should have code already indented as long as the source was.

我知道的唯一方法是使用System.Xmlouterxmlor 之类的属性innerxml。与源代码一样,这些属性应该已经缩进了代码。

回答by Jared Skarstedt

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.

这是一个旧线程,但我想分享我的骇人听闻的答案。我需要将 xml 发送到 php,但我无法发送任何其他内容。

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:

我想出的答案是将文件保存到磁盘,然后在其上运行获取内容。这回显了 xml 文本,仅此而已:

#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

在我的情况下,我将它发送回 PHP 并且它运行良好