XMLDocument.Save adds return carriages to XML when elements are blank
You control that through the XMLWriter within the Settings Property.
Check out this example along with the following references.http://msdn.microsoft.com/en-us/library/ms162618.aspx
Refernceshttp://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspxhttp://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.aspxhttp://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.newlinehandling.aspx
Probably too late, but I referred to the solution given by Arvo Bowen. Arvo's solution is in C#, I wrote the same in Powershell Syntax
# $dest_file is the path to the destination file$xml_dest = [XML] (Get-Content $dest_file)## Operations done on $xml_dest#$settings = new-object System.Xml.XmlWriterSettings$settings.CloseOutput = $true$settings.Indent = $true$writer = [System.Xml.XmlWriter]::Create($dest_file, $settings)$xml_dest.Save($writer)$writer.Close()
It solved my two problems:
- One, problem stated above i.e. newline character being added to null/empty values.
- Second, no end tag being created for null/empty values.ex:
<tag1>$null</tag1>
would actually be written in file as<tag />
Refer this thread: Can we force XmlWriter to issue <my-tag></my-tag> rather than <my-tag/>?