ADO to XML using Stream
Hi All,
I am trying to retrieve an ADO recordset and convert it to an XML stream using ADO 2.5 in a C++ component. Then, I want to return the XML version of the recordset to the component's clients.
I have found working samples in VB that accomplish this. Here is the sample code:
Dim rs as new Recordset
Dim rs2 as new Recordset
Dim c as new Connection
Dim s as new Stream
‘ Query the Titles table.
c.Open “provider=sqloledb;data source=mydb;initial catalog=pubs;user id=sa;password=“
rs.cursorlocation = adUseClient
rs.Open “select * from titles”, c, adOpenStatic
‘ Save to the file in the XML format. Note that if you don’t specify
‘ adPersistXML, a binary format (ADTG) will be used by default.
rs.Save “titles.sav”, adPersistXML
‘ Save the Recordset into the ADO Stream object.
rs.save s, adPersistXML
rs.Close
c.Close
I have also successfully saved a recordset in xml format to a file using VC++.
Any ideas on how to save the XML to a stream and pass stream text back to client in C++?
Any comments or suggestions are appreciated,
Therese
# 1 Re: ADO to XML using Stream
Theresa,
Now that you have the XML saved to a file, it's pretty easy to get the XML stream.
What you do is this:
1. Open the file using the IXMLDOMDocument::load interface to load the XML file 2. Get the document element with IXMLDOMDocument::get_documentElement.
3. With the IXMLDOMElement you get, you can get the XML by calling the IXMLDOMElement::get_xml from IXMLDOMNode which IXMLDOMElement inherits from.
4. You have the XML as a string now you can return it.
If by "stream" you mean IStream object you can create the IStream object write the string to the stream and then return the stream to the caller.
Here is some sample code, quick and dirty and not validated, but it will give you the basic idea and a working point.
IXMLDOMDocument* pXMLDoc = NULL;
IXMLDOMElement* pXMLRoot = NULL;
_variant_t varFilename = L"titles.sav";
VARBOOL vbLoaded = VARIANT_FALSE;
HRESULT hr = E_FAIL;
hr = CoCreateInstance(CLSID_DOMDocument,
NULL,
CLSCTX_INPROC,
IID_IXMLDOMDocument,
(LPVOID*)&pXMLDoc);
_ASSERT(NULL != pXMLDoc);
if (SUCCEEDED(hr) && pXMLDoc)
{
//
// load the XML string into the object
//
vbSuccess = pXMLDoc->load(varFilename, &vbLoaded);
if (VARIANT_TRUE == vbLoaded)
{
//
// get the XML root.
//
hr = pXMLDoc->get_documentElement(&pXMLRoot);
_ASSERT(NULL != pXMLRoot);
if (SUCCEEDED(hr) && pXMLRoot)
{
BSTR bstrXML = NULL;
hr = pXMLRoot->get_xml(&bstrXML);
if (SUCCEEDED(hr) && bstrXML)
{
// TODO: insert what you want to do with the string here
SysFreeString(bstrXML);
}
pXMLRoot->Release();
}
}
}
pXMLDoc->Release();