2014-04-25 28 views
0

試圖格式化我的xml文件,我找不到如何設置PowerShell來執行此操作。一切都引用了C++或C#:(當寫一個XML文件(powershell)時添加回車符

這是我希望它看起來:

<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

我喜歡它看起來很漂亮,我意識到它對功能沒有任何影響。 – user3433766

+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的工作。

相關問題