编写XML文件时添加回车符(powershell)

时间:2014-04-25 18:26:21

标签: xml powershell xmlwriter

尝试格式化我的xml文件,但我无法找到如何设置powershell来执行此操作。一切都引用C ++或C#:(

这就是我想要的样子:

    <Resource name="UserDatabase" auth="Container"
          type="org.apache.catalina.UserDatabase"
          description="User database that can be updated and saved"
          factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
          pathname="conf/tomcat-users.xml" />

但这就是它的样子:

        <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" />
你对吗?我在powershell中尝试了不同的方法,我不能让它添加空间。以下是看起来最有希望的,但它没有效果。

$settings = New-Object system.Xml.XmlWriterSettings 
$settings.OmitXmlDeclaration = $false 
$settings.NewLineOnAttributes = $true

$XmlWriter = New-Object System.Xml.XmlTextWriter($path,$null)

$xmlWriter.Formatting = 'Indented'
$xmlWriter.Indentation = 1
$XmlWriter.IndentChar = "`t"

感谢任何有想法的人!

1 个答案:

答案 0 :(得分:1)

也许这在powershell中是不可能的(实际上没有loading C# code

以下代码返回错误:

$settings = New-Object system.Xml.XmlWriterSettings;
$settings.Indent = $true;
$settings.OmitXmlDeclaration = $true;
$settings.NewLineOnAttributes = $true;

$path = 'c:\test\meh.xml'

Add-Type -AssemblyName 'System.Xml'

$writerClass = New-Object XML.XmlTextWriter $path, ([Text.Encoding]::Unicode)
$writer = New-Object ([System.Xml.XmlWriter]::Create($writerClass,$settings))

New-Object : Cannot find type [System.Xml.XmlCharCheckingWriter]: verify that the assembly containing this type is loaded.

XmlCharCheckingWriter is an internal class这可能是问题的原因?我不确定如何使用PowerShell来完成这项工作。