Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 286023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:35:57+00:00 2026-05-12T05:35:57+00:00

When an XMLDOMDocument saves itself, how can i get it to include the XML

  • 0

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

  • <?xml version="1.0" encoding="UTF-8" ?>
  • <?xml version="1.0" encoding="UTF-16" ?>
  • <?xml version="1.0" encoding="UCS-2" ?>
  • <?xml version="1.0" encoding="UCS-4" ?>
  • <?xml version="1.0" encoding="ISO-10646-UCS-2" ?>
  • <?xml version="1.0" encoding="UNICODE-1-1-UTF-8" ?>
  • <?xml version="1.0" encoding="UNICODE-2-0-UTF-16" ?>
  • <?xml version="1.0" encoding="UNICODE-2-0-UTF-8" ?>
  • <?xml version="1.0" encoding="US-ASCII" ?>
  • <?xml version="1.0" encoding="ISO-8859-1" ?>
  • <?xml version="1.0" encoding="WINDOWS-1250" ?>

The XMLDOMDomcument object is being created in memory (i.e. the xml is not being loaded from some outside source):

{
   IXMLDOMDocument2 doc = new DOMDocument60();

   //add nodes to the doc
   ...

   doc.Save(saveTarget);
}

Without the xml declaration you only get the body xml, e.g.:

<Customer>
   ...
</Customer>

rather than the full XML document:

<?xml version="1.0" encoding="US-ASCII" ?>
<Customer>
   ...
</Customer>

Question 2

How can i control the encoding the XMLDOMDocument will use when it saves to a stream?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:35:57+00:00Added an answer on May 12, 2026 at 5:35 am

    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("<doc><one>test1</one><two>test2</two></doc>")
    
    ' 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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.