XMLDocument.Save adds return carriages to XML when elements are blank

后端 未结 3 1431
刺人心
刺人心 2021-01-08 01:43

I\'m loading a XML Document that has some tags that have no innertext.

If I populate the innertext with some data then it works as needed (you get opening tag, inner

3条回答
  •  梦毁少年i
    2021-01-08 02:05

    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: $null would actually be written in file as Refer this thread: Can we force XmlWriter to issue rather than ?

提交回复
热议问题