How do you read xml from an in memory stream instead of a file stream

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello, 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.

<div style="color:Black;background-color:White; <pre>
HRESULT hr = S_OK; CComPtr<IStream> pStream = NULL;
IXmlReader *pReader = NULL; XmlNodeType nodeType;

LPWSTR pwszXMLString = L"<?xml version"1.0"
encoding=<span style="color:#A31515; "UTF-8" ?>rn"
L<span style="color:#A31515; "<paramlist name="LP rn"
L<span style="color:#A31515; "<value></value>rn" L<span style="color:#A31515; "<value></value>rn"
L<span style="color:#A31515; "</paramlist>rn" L"<param
name=<span style="color:#A31515; "AutoConnect false</param>rn" L"<param
name=<span style="color:#A31515; "ConnectWhenLit false</param>rn" L"<param
name=<span style="color:#A31515; "SessionMaxBytes 200000</param>rn" L"<param
name=<span style="color:#A31515; "SessionTimeoutSecs 300</param>rn" L"<param
name=<span style="color:#A31515; "PacketDelayMs 0</param>rn" L"<param
name=<span style="color:#A31515; "PacketSizeBytes 4096</param>rn" L"<param
name=<span style="color:#A31515; "LowSSLSecurity true</param>rn";

DWORD dwWritten = 0;
hr = CreateStreamOnHGlobal(NULL, FALSE, &pStream);
hr = pStream->Write(pwszXMLString, wcslen(pwszXMLString) * <span style="color:Blue; sizeof(WCHAR), &dwWritten);

<span style="color:Green; // 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*) <span style="color:Blue; new BYTE[cbSize + <span style="color:Blue; sizeof(WCHAR)];

<span style="color:Blue; if (pwszContent == NULL)
<span style="color:Blue; return E_OUTOFMEMORY;
pStream->Seek(pos, STREAM_SEEK_SET, NULL);
SIZE_T cbRead;
pStream->Read(pwszContent, cbSize, &cbRead);
pwszContent[cbSize/<span style="color:Blue; sizeof(WCHAR)] = L;

CZString czContent;
czContent.LoadWideString(pwszContent, cbSize);
wprintf(L<span style="color:#A31515; "%S", czContent.GetString().c_str());

pStream->Seek(pos, STREAM_SEEK_SET, NULL);
<span style="color:Blue; if (hr == S_OK)
{
<span style="color:Blue; typedef HRESULT (WINAPI *CreateXmlReaderFunc)(<span style="color:Blue; const IID & riid, <span style="color:Blue; void** ppvObject, IMalloc * pMalloc);

CreateXmlReaderFunc _CreateXmlReaderFunc = (CreateXmlReaderFunc)GetProcAddress(m_hXMLLite, <span style="color:#A31515; "CreateXmlReader");

<span style="color:Blue; if (FAILED(hr = _CreateXmlReaderFunc(__uuidof(IXmlReader), (<span style="color:Blue; void**) &pReader, NULL)))
{
MessageBox(NULL, CStringHelper::Format(L<span style="color:#A31515; "Error: GetProcAddress() failed to find CreateXmlReader %dn", GetLastError()).c_str(), L<span style="color:#A31515; "Error", MB_OK);
<span style="color:Blue; return -1;
}

pReader->SetInput(pStream);
}

<span style="color:Blue; while (S_OK == (hr = pReader->Read(&nodeType)))
{
<span style="color:Blue; switch (nodeType)
{
<span style="color:Green; // parse xml here
}
}

[/code]
Any idea as to why I am getting this error?

View the full article
 
Back
Top