Adds a new schema to the schema collection, associating the given namespace URI with the specified schema.
objXMLDOMSchemaCol.add(namespaceURI, var)
HRESULT add(BSTR namespaceURI, VARIANT var);
The empty string, "", will associate the schema with the empty namespace, xmlns="". This may be any string that can be used in an xmlns attribute, but it cannot contain entity references. The same white space normalization that occurs on the xmlns attribute also occurs on this parameter (that is, leading and trailing white space is trimmed, new lines are converted to spaces, and multiple adjacent white space characters are collapsed into one space).
This argument can be Null, which results in the removal of any schema for the specified namespaces. If the schema is an IXMLDOMNode, the entire document the node belongs to will be preserved.
If this call fails, the collection remains unchanged. E_FAIL is returned if:
If it was loading a schema and encountered a parse error, then the parse error reason is returned in the IErrorInfo. E_INVALIDARG is returned if the VARIANT argument contains an invalid value.
Schemas referenced by this schema are not added to the collection. The contents of the schema are added to the internal collection (the schema may be copied). If a schema is already in the collection with this namespace, it will be replaced with the new one being added.
BOOL DOMDocument2schemas() { BOOL bResult = FALSE; short sResult = FALSE; IXMLDOMNodeList *pIXMLDOMNodeList=NULL; IXMLDOMDocument2 *pIXMLDOMDocument2=NULL; IXMLDOMSchemaCollection *pIXMLDOMSchemaCollection=NULL; HRESULT hr; BSTR bstrValue; VARIANT varValue; try { hr=CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_SERVER, IID_IXMLDOMDocument2, (LPVOID*)(&pIXMLDOMDocument2)); SUCCEEDED(hr) ? 0 : throw hr; if(pIXMLDOMDocument2) { hr=pIXMLDOMDocument2->put_async(VARIANT_FALSE); if(SUCCEEDED(hr)) { hr=CoCreateInstance(CLSID_XMLSchemaCache, NULL, CLSCTX_SERVER, IID_IXMLDOMSchemaCollection, (LPVOID*)(&pIXMLDOMSchemaCollection)); SUCCEEDED(hr) ? 0 : throw hr; if(SUCCEEDED(hr) && pIXMLDOMSchemaCollection) { hr = pIXMLDOMSchemaCollection->add(_T("x-schema:books"), _variant_t(_T("d:\\inetpub\\wwwroot\\bookschema.xml"))); if(SUCCEEDED(hr)) { varValue.vt = VT_DISPATCH; varValue.pdispVal = pIXMLDOMSchemaCollection; hr=pIXMLDOMDocument2->putref_schemas(varValue); // The document will load only if a valid schema is // attached to the xml file. hr=pIXMLDOMDocument2->load(_variant_t( _T("d:\\inetpub\\wwwroot\\samplexmldtd.xml")), &sResult); if(SUCCEEDED(hr) && (sResult==VARIANT_TRUE)) { pIXMLDOMDocument2->get_xml(&bstrValue); ::MessageBox(NULL, bstrValue, _T("Loaded Document"), MB_OK); bResult=TRUE; } } RELEASE(pIXMLDOMSchemaCollection); } } RELEASE(pIXMLDOMDocument2); } } catch(...) { CHECK_AND_RELEASE(pIXMLDOMDocument2); CHECK_AND_RELEASE(pIXMLDOMNodeList); CHECK_AND_RELEASE(pIXMLDOMSchemaCollection); DisplayErrorToUser(); } return bResult; }
d:\\inetpub\\wwwroot\\samplexmldtd.xml
<?xml version='1.0'?> <COLLECTION xmlns="x-schema:books" xmlns:dt="urn:schemas-microsoft-com:datatypes"> <DATE dt:dt="datetime">1998-10-13T15:56:00</DATE> <BOOK> <TITLE>Cosmos</TITLE> <AUTHOR>Carl Sagan</AUTHOR> <PUBLISHER>Ballantine Books</PUBLISHER> </BOOK> <BOOK> <TITLE>Catwings</TITLE> <AUTHOR>Ursula K. Le Guin</AUTHOR> <PUBLISHER>Scholastic</PUBLISHER> </BOOK> <BOOK> <TITLE>Home Town</TITLE> <AUTHOR>Tracy Kidder</AUTHOR> <PUBLISHER>Random House</PUBLISHER> </BOOK> </COLLECTION>
d:\\inetpub\\wwwroot\\bookschema.xml
<?xml version="1.0"?> <Schema xmlns="urn:schemas-microsoft-com:xml-data"> <ElementType name="TITLE" /> <ElementType name="AUTHOR" /> <ElementType name="PUBLISHER" /> <ElementType name="DATE" /> <ElementType name="BOOK" model="closed"> <element type="TITLE" /> <element type="AUTHOR" /> <element type="PUBLISHER" /> </ElementType> <ElementType name="COLLECTION" model="closed"> <element type="BOOK" /> </ElementType> </Schema>
IXMLDOMSchemaCollection Interface