Windows apps
Collapse the table of content
Expand the table of content

Reading an Encoded XML Document Using XmlLite

This topic illustrates how to implement an XmlLite application that reads an XML document with encoding.

This example includes the following files:

To create the example

  1. Create a Visual Studio 2005 project. For details of how to do this, see Building XmlLite Applications.

  2. Copy the C++ source code for this example and paste it into your C++ source file.

  3. Build the example.

  4. Either create an encoded XML file and use it as the input to this sample, or copy the file that was output from the Writing an Encoded XML Document Using XmlLite sample. You can use various editors, including Visual Studio, to create an encoded XML document.

  5. Run the sample and verify that the output matches the output provided.

Programming Details

The following is a detailed list of what is required to implement an XmlLite application that reads an encoded document:

  • Implement a class that extends the IStream interface. This is required because the CreateXmlReader and CreateXmlWriter methods require your application to pass in an object that implements the IStream interface. In the example provided, the FileStream class is an example of a class that extends IStream.

  • Declare and instantiate this class by using the OpenFile factory method. Use the CComPtr class when declaring the variables that will contain the file reader variable. Using the CComPtr class allows XmlLite to use smart pointers, which handle many aspects of memory management. The following line of code shows using the CComPtr class:

    CComPtr<IStream> pOutFileStream;
  • Use the CreateXmlReader factory method to instantiate your XmlLite reader. In the example, the following code initializes the pReader variable:

    if (FAILED(hr = CreateXmlReader(__uuidof(IXmlReader),(void**) &pReader, NULL)))
        wprintf(L"Error creating xml reader, error is %08.8lx", hr);
        return -1;
  • Declare an instance of the IXmlReaderInput class. Use the CComPtr class when declaring the variable:

    CComPtr<IXmlReaderInput> pReaderInput;
  • Create an instance of IXmlReaderInput by using one of the two factory methods, CreateXmlReaderInputWithEncodingCodePage and CreateXmlReaderInputWithEncodingName. The factory methods take as an argument the input file stream that you previously created. In the example, it initializes the pReaderInput variable:

    if (FAILED(hr = CreateXmlReaderInputWithEncodingName(pFileStream, NULL, L"utf-16", FALSE,
        L"c:\temp", &pReaderInput)))
        wprintf(L"Error creating xml reader with encoding code page, error is %08.8lx", hr);
        return -1;
  • Use the SetInput method to set the input stream of the IXmlReader. Instead of setting this to an instance of the IStream class, set this to the instance of the IXmlReaderInput class.

    if (FAILED(hr = pReader->SetInput(pReaderInput)))
        wprintf(L"Error setting input for reader, error is %08.8lx", hr);
        return -1;
  • From this point, your application will read the document the same way it reads from a document that is not encoded.

  • If you set the XmlWriterProperty_MultiLanguage property, XmlLite will use that COM object to convert to the designated encoding. This allows your application to use encodings that are not supported natively.

To see the supported code pages and their associated encoding names, browse, and search for "Supported Code Pages (Application Center Test 1.0)".

© 2018 Microsoft