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

Writing an Encoded XML Document Using XmlLite

This topic describes how to implement an XmlLite application that writes an XML document with encoding. The topic provides a simple example that outputs an encoded XML file.

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. Run the sample and verify that the XML file created by your sample matches the output provided.

Programming Details

The following is a detailed list of what you need to do to implement an XmlLite application that writes 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 IStream. 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. This is better because XmlLite is able to use smart pointers, which handle many aspects of memory management. The following line of code shows how to use the CComPtr class:

    CComPtr<IStream> pOutFileStream;
  • Use the CreateXmlWriter factory method to instantiate your XmlLite writer. In the example provided, this initializes the pWriter variable:

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

    CComPtr <IXmlWriterOutput> pWriterOutput;
  • Create an instance of IXmlWriterOutput by using one of the two factory methods, CreateXmlWriterOutputWithEncodingCodePage and CreateXmlWriterOutputWithEncodingName. The factory methods take as an argument the output file stream that you previously created. In the example, it initializes the pWriterOutput variable:

    if (FAILED(hr = CreateXmlWriterOutputWithEncodingName(pOutFileStream, NULL, L"utf-16",
        wprintf(L"Error creating xml reader with encoding code page, error is %08.8lx", hr);
        return -1;
  • Use the SetOutput method to set the output stream of the IXmlWriter. Instead of setting this to an instance of the IStream class, set this to the instance of the IXmlWriterOutput class:

    if (FAILED(hr = pWriter->SetOutput(pWriterOutput)))
        wprintf(L"Error setting output for writer, error is %08.8lx", hr);
        return -1;
  • From this point, your application will write the document in the same way as when writing without an encoding page.

  • 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.

  • The example creates the instance of IXmlWriterOutput by using the CreateXmlWriterOutputWithEncodingName method. It also contains a commented out section of code that creates the instance of IXmlWriterOutput by using the CreateXmlWriterOutputWithEncodingCodePage method. You can experiment with both factory methods to create output files with a variety of encodings.

This example generates the encoded XML document that the Reading an Encoded XML Document Using XmlLite example reads.

To see the supported code pages, and their associated encoding names, see Application Test Center Supported Code Pages.

© 2017 Microsoft