Microsoft KB Archive/282287

= PRB: Encoding Attribute Is Not Returned in DOMDocument XMLProperty =

Article ID: 282287

Article Last Modified on 10/12/2001

-

APPLIES TO


 * Microsoft XML Parser 2.0
 * Microsoft XML Parser 2.5
 * Microsoft XML Parser 2.6
 * Microsoft XML Core Services 4.0
 * Microsoft XML Parser 3.0 Service Pack 1
 * Microsoft XML Core Services 4.0

-



This article was previously published under Q282287



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 INFO: MSXML 4.0 Specific GUIDs and ProgIds

Steps To Reproduce Behavior
 Create an XML file (&quot;test.xml&quot;) similar to the following text that specifies a particular encoding, in this case &quot;windows-1252:&quot;

 Hello

  Create a script using the following code:    Set xmldoc = CreateObject(&quot;Msxml2.DOMDocument&quot;) xmldoc.async = false xmldoc.load(&quot;test.xml&quot;) MsgBox xmldoc.xml   </li> Execute the script, and note the XML that is displayed.</li></ol>

Results
The XML data that is displayed in the message box looks similar to the following:

<pre class="fixed_text"><?xml version=&quot;1.0&quot;?> Hello

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 &quot;encoding&quot; attribute.

The following Microsoft VBScript example displays the value &quot;windows-1252&quot; 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(&quot;encoding&quot;).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(&quot;encoding&quot;)->Gettext;

<div class="references_section">