Microsoft Knowledge Base Email Alertz

The

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 282287 - Last Review: October 12, 2001 - Revision: 1.2

PRB: Encoding Attribute Is Not Returned in DOMDocument XMLProperty

This article was previously published under Q282287

On This Page

SYMPTOMS

The xml property of the DOMDocument object does not return the encoding attribute for the XML data, even if a specific encoding is specified in the XML.

CAUSE

Because the xml property always returns the data as a Unicode string, it is UTF-16 encoded. This means that the original encoding is no longer valid and is filtered out.

STATUS

This behavior is by design.

MORE INFORMATION

If a newer version of MSXML has been installed in side-by-side mode, you must explicitly use the Globally Unique Identifiers (GUIDs) or ProgIDs for that version to run the sample code. For example, MSXML version 4.0 can only be installed in side-by-side mode. For additional information about the code changes that are required to run the sample code with the MSXML 4.0 parser, click the following article number to view the article in the Microsoft Knowledge Base:
305019  (http://kbalertz.com/Feedback.aspx?kbNumber=305019/EN-US/ ) INFO: MSXML 4.0 Specific GUIDs and ProgIds

Steps To Reproduce Behavior

  1. Create an XML file ("test.xml") similar to the following text that specifies a particular encoding, in this case "windows-1252:"
    <?xml version="1.0" encoding="windows-1252"?>
    <root>Hello</root>
    						
  2. Create a script using the following code:
    <HTML>
    <BODY>
      <script language="vbscript">
    	Set xmldoc = CreateObject("Msxml2.DOMDocument")
    	xmldoc.async = false
    	xmldoc.load("test.xml")
    	MsgBox xmldoc.xml
      </script>
    </BODY>
    </HTML>
    					
  3. Execute the script, and note the XML that is displayed.

Results

The XML data that is displayed in the message box looks similar to the following:
<?xml version="1.0"?>
<root>Hello</root>
					
Note that the encoding attribute has been removed.

However, the original value of this attribute is still stored in the DOMDocument, and can be retrieved by using a XMLDOMProcessingInstruction object. Usually, the encoding information is contained in the beginning of the XML file, or as the first node of the DOMDocument.

To retrieve the encoding information, retrieve the first node (item 0) of the DOMDocument object, which, in this case, is a processing instruction node, and then get the text value of the corresponding "encoding" attribute.

The following Microsoft VBScript example displays the value "windows-1252" if xmldoc refers to a DOMDocument object that was created by using the XML data from the preceding example:
Dim encoding
encoding = xmldoc.childNodes(0).Attributes.getNamedItem("encoding").Text
MsgBox encoding
				
The following is an example of how to retrieve the value in Microsoft Visual C++:
	IXMLDOMProcessingInstructionPtr pInst = pXMLDoc->GetchildNodes()->Getitem(0);
	_bstr_t bstrEncoding = pInst->Getattributes()->getNamedItem("encoding")->Gettext();
				

APPLIES TO
  • Microsoft XML Parser 2.0
  • Microsoft XML Parser 2.5
  • Microsoft XML Parser 2.6
  • Microsoft XML Parser 3.0
  • Microsoft XML Parser 3.0 Service Pack 1
  • Microsoft XML Core Services 4.0
Keywords: 
kbprb KB282287
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate