opening/saving xml while preserving newline between node's attributes

后端 未结 2 1119
忘了有多久
忘了有多久 2021-01-13 08:49

This is what I have so far:

$XML = New-Object System.Xml.XmlDocument
$XML.PreserveWhitespace = $true
$XML.Load($path)
#change some node attributes
$XML.Save(         


        
相关标签:
2条回答
  • 2021-01-13 09:08

    Please have a look at this answer to a question very similar to yours:

    While there doesn't seem to be a way of preserving xml attribute formatting, you can define it yourself on the xml document by using the power of the XmlWriterSettings and XmlWriter classes.

    You can specify it to have newlines between attributes like so:

    $xwSettings = new-object System.Xml.XmlWriterSettings
    $xwSettings.NewLineOnAttributes = $true
    

    You then save the document using an XmlWriter together with these settings:

    $xmlWriter = [Xml.XmlWriter]::Create("c:\temp\newlines.xml", $xwSettings)
    $doc.Save($xmlWriter)
    

    (Code is all from original answer. Kudos to vonPryz)

    0 讨论(0)
  • 2021-01-13 09:34

    No XML parser in the world is going to tell you whether the attributes in a start tag are separated by spaces, newlines, or tabs. You aren't supposed to care.

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