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($path)
If I take open an XML file, and take a snippet like this:
<Node
Name="tyjytj"
Number="rthjr"
Source="rjyrtjrjrtj"
BinaryDrive="teheherhehtr" />
It will save it like this:
<Node Name="tyjytj" Number="rthjr" Source="rjyrtjrjrtj" BinaryDrive="teheherhehtr" />
But I want to be able to change (for example) the Name of the Node, while keeping newlines between each attribute. I want the format to remain exactly how it was before I open/saved it.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With