addCollection Method

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - DOM Reference

addCollection Method

Adds schemas from another collection into the current collection and replaces any schemas that collide on the same namespace URI.

[Script]

Script Syntax

objXMLDOMSchemaCol.addCollection(objXMLDOMSchemaCollection);

Parameters

objXMLDOMSchemaCollection
The collection containing the schemas to add.

Example

var xmldoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.5.0");
var SchemaCache = new ActiveXObject("Msxml2.XMLSchemaCache.5.0");
var SchemaCache2 = new ActiveXObject("Msxml2.XMLSchemaCache.5.0");

xmldoc.async = false;
xmldoc.validateOnParse = false;
SchemaCache.add("x-schema:books", "c:\\books.xsd");
SchemaCache2.addCollection(SchemaCache);
SchemaCache2.add("x-schema:books", "c:\\NewBooks.xsd");
xmldoc.schemas = SchemaCache2;
// The document will load only if a valid schema is attached to the xml
// file. The new schema will override the old one
xmldoc.load("c:\\books.xml");
if (xmlDoc.parseError.errorCode != 0) {
   var myErr = xmlDoc.parseError;
   alert("You have error " + myErr.reason);
} else {
   alert(xmldoc.xml) ;
}
[Visual Basic]

Visual Basic Syntax

objXMLDOMSchemaCol.addCollection(objXMLDOMSchemaCollection)

Parameters

objXMLDOMSchemaCollection
The collection containing the schemas to add.

Example

Dim xmldoc As New Msxml2.FreeThreadedDOMDocument50
Dim SchemaCache As New Msxml2.XMLSchemaCache50
Dim SchemaCache2 As New Msxml2.XMLSchemaCache50

xmldoc.async = False
xmldoc.validateOnParse = True
SchemaCache.Add "x-schema:books", "c:\books.xsd"
SchemaCache2.addCollection SchemaCache
SchemaCache2.Add "x-schema:books", "c:\newbooks.xsd"
Set xmldoc.schemas = SchemaCache2
' The document will load only if a valid schema is attached to the xml
' file. The new schema will override the old one.
xmldoc.Load "c:\books.xml"
If (xmlDoc.parseError.errorCode <> 0) Then
   Dim myErr
   Set myErr = xmlDoc.parseError
   MsgBox("You have error " & myErr.reason)
Else
   MsgBox xmldoc.xml
End If
[C/C++]

C/C++ Syntax

HRESULT addCollection(IXMLDOMSchemaCollection * otherCollection);

Parameters

otherCollection [in]
The collection containing the schemas to add.

C/C++ Return Values

S_OK
The value returned if the collection is added successfully.
E_FAIL is returned if:
  • The collection is read-only.
  • The document is not a recognized schema.
  • An error occurs when compiling the schema.
  • The ready state of the document is not 4.
E_POINTER
The value returned if input pointer is invalid.

Example

#include "stdafx.h"
#include "tchar.h"
#import "c:\\winnt\\system32\\msxml5.dll"
using namespace MSXML2;

void AddCollectionSample();

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
   ::CoInitialize(NULL);
   AddCollectionSample();
   ::CoUninitialize();
   return 0;
}

void AddCollectionSample()
{
   IXMLDOMDocument2Ptr pIXMLDOMDocument2;
   IXMLDOMSchemaCollection2Ptr pIXMLDOMSchemaCollection2Ptr;
   IXMLDOMSchemaCollection2Ptr pIXMLDOMSchemaCollection2Ptr2;
   int nResult;
   
   try
   {
   // Create the DOM
      nResult = pIXMLDOMDocument2.CreateInstance(__uuidof(MSXML2::DOMDocument50));
      (nResult == 0) ? 0: throw nResult;

   // Create the Schema Collections
      nResult = pIXMLDOMSchemaCollection2Ptr.CreateInstance(__uuidof(MSXML2::XMLSchemaCache50));
      (nResult == 0) ? 0: throw nResult;
      nResult = pIXMLDOMSchemaCollection2Ptr2.CreateInstance(__uuidof(MSXML2::XMLSchemaCache50));
      (nResult == 0) ? 0: throw nResult;
   
   // Add the schema to the collection
      nResult = pIXMLDOMSchemaCollection2Ptr->add(_T("x-schema:books"), _T("c:\\books.xsd"));
      (nResult == 0) ? 0: throw nResult;
      
   // attach all schemas from "pIXMLDOMSchemaCollection2Ptr"
      nResult = pIXMLDOMSchemaCollection2Ptr2->addCollection(pIXMLDOMSchemaCollection2Ptr.GetInterfacePtr());
      (nResult == 0) ? 0: throw nResult;

   // override the old schema
      nResult = pIXMLDOMSchemaCollection2Ptr2->add(_T("x-schema:books"), _variant_t(_T("c:\\Newbooks.xsd")));
      (nResult == 0) ? 0: throw nResult;
      

   // Attach schemas
      pIXMLDOMDocument2->schemas = pIXMLDOMSchemaCollection2Ptr2.GetInterfacePtr();

      pIXMLDOMDocument2->async = false;
      pIXMLDOMDocument2->validateOnParse = true;

   // Load the document into the DOM
      nResult = pIXMLDOMDocument2->load(_T("c:\\books.xml"));
      (nResult == -1) ? 0: throw nResult;

      ::MessageBox(NULL, pIXMLDOMDocument2->xml, _T("Loaded Document"), MB_OK);
   } catch(...)
   {
      ::MessageBox(NULL, _T("Sample Failed"), _T("Error"), MB_OK);
   }
}

File Name: c:\books.xml

<?xml version='1.0'?>
<Collection xmlns="x-schema:books">
   <Book>
      <Title>Lover Birds</Title>
      <Author>Cynthia Randall</Author>
      <Publisher>Lucerne Publishing</Publisher>
   </Book>
</Collection>

File Name: c:\books.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
   <xs:element name="Collection">
      <xs:complexType>
    <xs:sequence>
       <xs:element name="OtherBook">
          <xs:complexType>
        <xs:sequence>
         <xs:element name="Title" type="xs:string"/>
         <xs:element name="Author" type="xs:string"/>
         <xs:element name="Publisher" type="xs:string"/>
        </xs:sequence>
          </xs:complexType>
       </xs:element>
    </xs:sequence>
      </xs:complexType>
   </xs:element>
</xs:schema>

File Name: c:\NewBooks.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
   <xs:element name="Collection">
      <xs:complexType>
    <xs:sequence>
       <xs:element name="Book">
          <xs:complexType>
        <xs:sequence>
         <xs:element name="Title" type="xs:string"/>
         <xs:element name="Author" type="xs:string"/>
         <xs:element name="Publisher" type="xs:string"/>
        </xs:sequence>
          </xs:complexType>
       </xs:element>
    </xs:sequence>
      </xs:complexType>
   </xs:element>
</xs:schema>

Output (in a message box)

<?xml version='1.0'?>
<Collection xmlns="x-schema:books">
  <Book>
    <Title>Lover Birds</Title>
      <Author>Cynthia Randall</Author>
      <Publisher>Lucerne Publishing</Publisher>
  </Book>
</Collection>

Remarks

There is no guarantee that the two collections will return exactly the same schema. The collection is free to clone them, if necessary.

Adding a collection to itself has no effect.

To view reference information for Visual Basic, C/C++, or Script only, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Applies to: IXMLDOMSchemaCollection/XMLSchemaCache