how to load xml schema from resource?
-
Hi I want to include an xml validation schema as a part of an executable. My problem is how to add the schema to the MSXML2::XMLSchemaCache40. I have a schema called 'setup.xsd', and m_schemaCache is a MSXML2::IXMLDOMSchemaCollection2 interface to XMLSchemaCache40. The following code works ('setup' is the namespace for the schema).
hr = m_schemaCache->add(L"setup", L"Setup.xsd");
Now I want to load the schema from a resource. Since IXMLDOMSchemaCollection2::add only accepts url's or MSXML2::DOMDocument's, I decided to parse the xml data using DOMDocument, and then pass the DOMDocument over to the XMLSchemaCache40. In the following code, everything seems to be ok until I try to add the DOMDocument to the XMLSchemaCache40. The resource is loaded, The DOMDocument parses it, and no errors are found. The readyState of the DOMDocument is 4.//Create resource name from resource id wchar_t sResName[10]; swprintf_s(sResName, 10, L"#%d", resourceId); //Locate the resource TCHAR sRestype[13] = _T("SCHEMA"); HRSRC hres = FindResource(NULL, sResName, sRestype); if (hres == 0) { return false; } //If resource is found a handle to the resource is returned //now just load the resource HGLOBAL hbytes = LoadResource(NULL, hres); // Lock the resource LPVOID pdata = LockResource(hbytes); //Convert the resource text file to data we can use LPBYTE sData = (LPBYTE)pdata; BSTR sXml = _bstr_t((char*) sData); //Crate instance of DOM parser MSXML2::IXMLDOMDocument* doc; hr = CoCreateInstance(__uuidof(MSXML2::DOMDocument), NULL, CLSCTX_ALL, __uuidof(MSXML2::IXMLDOMDocument), (void**) &doc); if (FAILED(hr)) return false; //We don't want async read doc->put_async(VARIANT_FALSE); doc->put_validateOnParse(VARIANT_TRUE); //Parse the xml schema we just loaded from our resources VARIANT_BOOL succeeded; hr = doc->loadXML(sXml, &succeeded); if (succeeded != VARIANT_TRUE) { //Load failed return false; } //HERE IT FAILS!!!!!! //Add the loaded schema to the schema collection hr = m_schemaCache->add(bstrNamespace, _variant_t(doc)); if ( FAILED(hr) ) return false; return true;
What am I doing wrong? Thanks in advance for any answers :) øivind