Search code examples
c++xmlreaderxmllite

Unable to read xml string with xmllite using memory buffer


I am trying to open an in memory stream for use with the xmllite library. Writing to one works ok, but reading from one is giving me a hard time. Below is the code that I am using. Basically I create a default xml string (LPWSTR) and write it to a memory stream using CreateStreamOnHGlobal. I then seek to the beginning and read it back to make sure its in there (it is). Then I seek back again and assign it to the input of the reader. It never gets past the line:

while (S_OK == (hr = pReader->Read(&nodeType)))

I get an XmlNodeType_None and an HRESULT value of -1072894427. I believe it is having trouble reading the stream, but I dont know for sure. The same code works fine if I use a filestream instead and writing to the xml from the memory stream works as well

HRESULT hr = S_OK;  CComPtr<IStream> pStream = NULL;
IXmlReader *pReader = NULL;
XmlNodeType nodeType;

LPWSTR pwszXMLString = 
    L"<?xml version\"1.0\" encoding=\"UTF-8\" ?>\r\n"
    L"<paramlist name=\"LP\">\r\n"
        L"<value></value>\r\n"
        L"<value></value>\r\n"
    L"</paramlist>\r\n"
    L"<param name=\"AutoConnect\">false</param>\r\n"
    L"<param name=\"ConnectWhenLit\">false</param>\r\n"
    L"<param name=\"SessionMaxBytes\">200000</param>\r\n"
    L"<param name=\"SessionTimeoutSecs\">300</param>\r\n"
    L"<param name=\"PacketDelayMs\">0</param>\r\n"
    L"<param name=\"PacketSizeBytes\">4096</param>\r\n"
    L"<param name=\"LowSSLSecurity\">true</param>\r\n";

DWORD dwWritten = 0;
hr = CreateStreamOnHGlobal(NULL, FALSE, &pStream);
hr = pStream->Write(pwszXMLString, wcslen(pwszXMLString) * sizeof(WCHAR), &dwWritten);

// print out the contents of the memory stream just to make sure we have it
LARGE_INTEGER pos;
pos.QuadPart = 0;
pStream->Seek(pos, STREAM_SEEK_SET, NULL);
STATSTG ssStreamData = {0};
pStream->Stat(&ssStreamData, STATFLAG_NONAME);
SIZE_T cbSize = ssStreamData.cbSize.LowPart;
LPWSTR pwszContent = (WCHAR*) new BYTE[cbSize + sizeof(WCHAR)];
if (pwszContent == NULL)
    return E_OUTOFMEMORY;

pStream->Seek(pos, STREAM_SEEK_SET, NULL);
SIZE_T cbRead;
pStream->Read(pwszContent, cbSize, &cbRead);
pwszContent[cbSize/sizeof(WCHAR)] = L'\0';

CZString czContent;
czContent.LoadWideString(pwszContent, cbSize);
wprintf(L"%S", czContent.GetString().c_str());
pStream->Seek(pos, STREAM_SEEK_SET, NULL);

if (hr == S_OK)
{
    typedef HRESULT (WINAPI *CreateXmlReaderFunc)(const IID & riid, void** ppvObject, IMalloc * pMalloc);
    CreateXmlReaderFunc _CreateXmlReaderFunc = (CreateXmlReaderFunc)GetProcAddress(m_hXMLLite, "CreateXmlReader");

    if (FAILED(hr = _CreateXmlReaderFunc(__uuidof(IXmlReader), (void**) &pReader, NULL)))
    {
        MessageBox(NULL, CStringHelper::Format(L"Error: GetProcAddress() failed to find 'CreateXmlReader' %d\n", GetLastError()).c_str(), L"Error", MB_OK);
        return -1;
    }

    pReader->SetInput(pStream);
}

while (S_OK == (hr = pReader->Read(&nodeType))) 
{
    switch (nodeType) 
    {
                    // parse xml here
    }
}
return 0;

Solution

  • Your xml string is not correct. There must be "=" between "version" and "1.0". Secondly, the string uses UTF-16, while the header states it is UTF-8. Change UTF-8 to UTF-16 in header or remove encoding attribute. Either

    LPWSTR pwszXMLString = 
    L"<?xml version=\"1.0\" encoding=\"UTF-16\" ?>\r\n"
    

    or

    LPWSTR pwszXMLString = 
    L"<?xml version=\"1.0\"?>\r\n"
    

    works.