How to make XMLDOMDocument include the XML Declaration?

后端 未结 5 1155
小鲜肉
小鲜肉 2021-01-12 06:29

When an XMLDOMDocument saves itself, how can i get it to include the XML Declaration, e.g.:

5条回答
  •  耶瑟儿~
    2021-01-12 07:07

    You need to use a MXXMLWriter60, instead of saving it directly. Sorry I don't have a C# example, but here is the VB.Net equivalent. See IMXWriter for details.

    ' Create and load a DOMDocument object.
    
    Dim xmlDoc As New DOMDocument60
    xmlDoc.loadXML("test1test2")
    
    ' Set properties on the XML writer - including BOM, XML declaration and encoding
    
    Dim wrt As New MXXMLWriter60
    wrt.byteOrderMark = True
    wrt.omitXMLDeclaration = False
    wrt.encoding = "US-ASCII"
    wrt.indent = True
    
    ' Set the XML writer to the SAX content handler.
    
    Dim rdr As New SAXXMLReader60
    Set rdr.contentHandler = wrt
    Set rdr.dtdHandler = wrt
    Set rdr.errorHandler = wrt
    rdr.putProperty "http://xml.org/sax/properties/lexical-handler", wrt
    rdr.putProperty "http://xml.org/sax/properties/declaration-handler", wrt
    
    ' Now pass the DOM through the SAX handler, and it will call the writer
    
    rdr.parse xmlDoc
    
    ' Let the writer do its thing
    
    Dim iFileNo As Integer
    iFileNo = FreeFile
    Open App.Path + "\saved.xml" For Output As #iFileNo
    Print #iFileNo, wrt.output
    Close #iFileNo
    

提交回复
热议问题