如何使用xmlite在缓冲区中存储xml? [英] how can i store xml in buffer using xmlite?
问题描述
我想写缓冲区XmlLite xml数据,但不能有任何api。写一个xml文件工作完美,但在内存流我无法计算
i我正在工作链接api
http://msdn.microsoft.com/en-us/library/ms752901(v = VS.85).aspx p>
I'm trying to write xml data with XmlLite on buffer but couldn't got any api. writing a xml file works perfect but on a memory stream I can't figure i am working on follwong link api http://msdn.microsoft.com/en-us/library/ms752901(v=VS.85).aspx
推荐答案
您可以使用 SHCreateMemStream 或 CreateStreamOnHGlobal 创建内存流。以下是您提供的示例代码:
You can use either SHCreateMemStream or CreateStreamOnHGlobal to create an memory stream. Here is the sample code for your reference:
CComPtr<IStream> spMemoryStream;
CComPtr<IXmlWriter> spWriter;
CComPtr<IXmlWriterOutput> pWriterOutput;
// Opens writeable output stream.
spMemoryStream.Attach(::SHCreateMemStream(NULL, 0));
if (spMemoryStream == NULL)
return E_OUTOFMEMORY;
// Creates the xml writer and generates the content.
CHKHR(::CreateXmlWriter(__uuidof(IXmlWriter), (void**) &spWriter, NULL));
CHKHR(::CreateXmlWriterOutputWithEncodingName(spMemoryStream,
NULL, L"utf-16", &pWriterOutput));
CHKHR(spWriter->SetOutput(pWriterOutput));
CHKHR(spWriter->SetProperty(XmlWriterProperty_Indent, TRUE));
CHKHR(spWriter->WriteStartDocument(XmlStandalone_Omit));
CHKHR(spWriter->WriteStartElement(NULL, L"root", NULL));
CHKHR(spWriter->WriteWhitespace(L"\n"));
CHKHR(spWriter->WriteCData(L"This is CDATA text."));
CHKHR(spWriter->WriteWhitespace(L"\n"));
CHKHR(spWriter->WriteEndDocument());
CHKHR(spWriter->Flush());
// Allocates enough memeory for the xml content.
STATSTG ssStreamData = {0};
CHKHR(spMemoryStream->Stat(&ssStreamData, STATFLAG_NONAME));
SIZE_T cbSize = ssStreamData.cbSize.LowPart;
LPWSTR pwszContent = (WCHAR*) new(std::nothrow) BYTE[cbSize + sizeof(WCHAR)];
if (pwszContent == NULL)
return E_OUTOFMEMORY;
// Copies the content from the stream to the buffer.
LARGE_INTEGER position;
position.QuadPart = 0;
CHKHR(spMemoryStream->Seek(position, STREAM_SEEK_SET, NULL));
SIZE_T cbRead;
CHKHR(spMemoryStream->Read(pwszContent, cbSize, &cbRead));
pwszContent[cbSize / sizeof(WCHAR)] = L'\0';
wprintf(L"%s", pwszContent);
XmlLite的一个漂亮的事情是它与IStream接口。它真的不在乎流是什么样的背后的场景。
One pretty thing about XmlLite is it works with IStream interface. It really doesn't care what exactly the stream looks like behind the scene.
这篇关于如何使用xmlite在缓冲区中存储xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!