I am using the XmlLite library to create an XML file. I want the resulting XML file's prolog to not include the encoding attribute (just the version):
<?xml version="1.0"?>
Here is my code:
HRESULT hr = S_OK;
IStream *pOutFileStream = NULL;
IXmlWriter *pWriter = NULL;
CComPtr<IXmlWriterOutput> pWriterOutput;
//Open writeable output stream
if (FAILED(hr = SHCreateStreamOnFile(output_file_name, STGM_CREATE | STGM_WRITE, &pOutFileStream)))
{
wprintf(L"Error creating file writer, error is %08.8lx", hr);
HR(hr);
}
if (FAILED(hr = CreateXmlWriter(__uuidof(IXmlWriter), (void**) &pWriter, NULL)))
{
wprintf(L"Error creating xml writer, error is %08.8lx", hr);
HR(hr);
}
if(FAILED(CreateXmlWriterOutputWithEncodingName(pOutFileStream, NULL, L"UTF-8", &pWriterOutput))){
wprintf(L"Error setting xml encoding, error is %08.8lx", hr);
HR(hr);
}
if (FAILED(hr = pWriter->SetOutput(pWriterOutput)))
{
wprintf(L"Error, Method: SetOutput, error is %08.8lx", hr);
HR(hr);
}
if (FAILED(hr = pWriter->SetProperty(XmlWriterProperty_Indent, 4)))
{
wprintf(L"Error, Method: SetProperty XmlWriterProperty_Indent, error is %08.8lx", hr);
HR(hr);
}
if (FAILED(hr = pWriter->WriteStartDocument(XmlStandalone_Omit)))
{
wprintf(L"Error, Method: WriteStartDocument, error is %08.8lx", hr);
HR(hr);
}
I have tried removing the call to CreateXmlWriterOutputWithEncodingName()
but even then a default encoding attribute with UTF-8 is getting created.
I have also tried putting NULL
as the third parameter to that function.
Assistance would be much appreciated!
The XML declaration is written by the WriteStartDocument
method.
Instead of calling WriteStartDocument
, you can call WriteProcessingInstruction
with L"xml"
as the name of the processing instruction to write the XML declaration the way you want, for example:
if (FAILED(hr = pWriter->WriteProcessingInstruction(L"xml", L"version=\"1.0\"")))
{
wprintf(L"Error, Method: WriteProcessingInstruction, error is %08.8lx", hr);
HR(hr);
}
This will write the XML declaration as <?xml version="1.0"?>
.