Click here to Skip to main content
12,950,027 members (81,543 online)
Click here to Skip to main content
Add your own
alternative version


25 bookmarked
Posted 26 Sep 2002

XML by Schema

, 29 Sep 2002
Rate this:
Please Sign up or sign in to vote.
Validate XML with XML schema.

Sample Image - sample.jpg


This application is used to validate XML files against XML schemas. This requires the MSXML library (4.0 or above); be aware of the bugs and update to the latest version or Service Pack!


The Collection

Is a storage where you can add <namespace, schema> pairs (check MSDN on the subject of XML schemas and IXMLDOMSchemaCollection for more information). Basically, you provide a namespace related to a schema. The namespace will be unique in the collection while the schema should appear in several distinct pairs.


Take a look at this box after every operation, to see if it succeeded or not.

Insert Pair

Write the URN of the schema in the uppermost edit box. You can open it from the local machine, pressing Open. Write the namespace in its edit place (the combo). Press Insert into Collection. Notice that the namespace could be an empty string or one made entirely by spaces. Inserting a namespace that already exists in the collection will update the content of the schema associated. You have to do it any time you modify the schema file. If you fail to insert a schema for a namespace that already exists in the collection, the last successfully inserted will remain as active.

Remove Pair

Is done using only the namespace. Type it or choose it from the combo and press Remove from Collection.

Remove All Pairs

Just press Empty entire Collection.

Validate XML

Write the URN of the XML file in the uppermost edit box or Open it. Press Validate as XML.

The Browser

Is used to display the content of the files involved into the validation process. You can also use it as with IE (view and modify the source, for example).

IXMLDOMSchemaCollection As Used

The code is from ValidatorDlg.cpp.


BOOL CValidatorDlg::OnInitDialog()
  // ...
  HRESULT hr2, hr1=m_doc.CoCreateInstance(__uuidof(DOMDocument));
  if (hr1!=S_OK || hr2!=S_OK)
    MessageBox("Creating COM objects failure!\r\n"
      "Make sure you have installed MSXML 4.0 on your machine!",
  VARIANT var;
  var.vt = VT_DISPATCH;
  var.pdispVal = m_schema.p;
  if (m_doc->putref_schemas(var)!=S_OK) // associate schema with document
    MessageBox("COM operation failed!", "XMLbyXSD");
  // ...

Pair Insertion

void CValidatorDlg::OnButtonAdd() 
  CString szMan, szUrn;
  // ...
    // adding <namespace, schema> pair to collection
    m_schema->add(_bstr_t(szMan), _variant_t(szUrn)); 
    // ...
  catch (_com_error &x) // attempt failed!  
    PrintStatus("Schema error!", (char *)x.Description(), 
      0, NULL); 

Namespace Removing

void CValidatorDlg::OnButtonRemove() 
  CString szMan;
  // ...
      // remove namespace+schema from collection
    catch (_com_error &x) // attempt failed
      PrintStatus("Removing schema error!", (char *)x.Description(), 
        0, NULL);
  // ...


void CValidatorDlg::OnButtonValidate() 
  BSTR bs;
  CString szReason, szText;
  // ...
  // associated schema collection will accomplish its task
  IXMLDOMParseError *pErr=NULL;
  if (m_doc->get_parseError(&pErr)==S_OK)
    if (pErr)
      if (ln) // errors
        // ...
        PrintStatus("XML is valid according to "
        "collection content!", NULL, ln, NULL);


  • September 30, 2002 - Bug fixed.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Hans Ruck
Software Developer (Senior)
Europe Europe
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralIXMLDOMSchemaCollection::add() may fail() Pin
Aaron Heusser7-Sep-04 12:12
memberAaron Heusser7-Sep-04 12:12 
I encountered a problem using the "Insert into Collection" functionality provided in the sample application to insert an XSD schema. I would always receive the following message:

Schema error!
Reason: Incorrect definition for the root element in schema.

Read on if you want to know more ...

The underlying failure occurs during the call to IXMLDOMSchemaCollection::add() (found in ValidatorDlg.cpp). This call would return an E_FAIL on my machine. (NOTE: according to MSDN, "Incorrect definition for the root element in schema." is the English description for the SCHEMA_SCHEMAROOT_EXPECTED parse error message.)

The problem appears to be that the original code for this article uses version independent class id's for DOMDocument and XMLSchemaCache. Version independent class id's were deprecated as of the MSXML 4.0 RTM. Therefore, unless you have some non-standard XML installation (e.g., the MSXML 4.0 April Technical Preview), then your version independent class id's likely map to MSXML 3.0 objects, rather than their 4.0 counterparts. Apparently, IXMLDOMSchemaCollection::add() does not function properly with MSXML 3.0.

One way to address the problem I described above is to change the two CoCreateInstance() lines for DOMDocument and XMLSchemaCache in CValidatorDlg::OnInitDialog() (found in ValidatorDlg.cpp) to explicitly use the 4.0 versions (e.g., DOMDocument40 and XMLSchemaCache40).

The truly confusing part of this issue is that it probably works as-is on some machines which have their version independent class id's mapped to MSXML 4.0. From my understanding this could happen if the MSXML 4.0 April Technical Preview was installed on the machine because the deprecation of the version independent id's was not implemented until the actual RTM of MSXML 4.0.

Here are links to a few articles that helped me wrap my mind around the issue:
What's New in the July 2001 Microsoft XML Parser 4.0 Technology Preview[^]
What's New in the October 2001 Microsoft XML Core Services (MSXML) 4.0 Release[^]
THE XML FILES - What's New in MSXML 4.0[^]
GeneralRe: IXMLDOMSchemaCollection::add() may fail() Pin
Bogdan Rechi7-Sep-04 22:29
memberBogdan Rechi7-Sep-04 22:29 
GeneralRe: IXMLDOMSchemaCollection::add() may fail() Pin
Aaron Heusser8-Sep-04 4:07
memberAaron Heusser8-Sep-04 4:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170524.1 | Last Updated 30 Sep 2002
Article Copyright 2002 by Hans Ruck
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid