Performs runtime validation on the currently loaded document using the currently loaded DTD, schema, or schema collection.
Visual Basic Syntax
objXMLDOMDocument2.validate
C/C++ Syntax
HRESULT validate();
C/C++ Return Values
IErrorInfo containing a formatted error message indicating exactly what went wrong, and one of the following HRESULTs (also returned in the ErrorInfo.Number property).
Return Value | Hexadecimal Value | Description |
E_PENDING | 0x8000000A | Document readyState is not 4 (indicating the document is not completely loaded). |
XML_E_INVALID | 0xC00CE225 | The document is well-formed, but is not valid according to the DTD. |
XML_E_NODTD | 0xC00CE224 | The document is well formed, but there is no DTD so there is no way to determine if it's valid. |
XML_E_NOTWF | 0xC00CE223 | The document is not well-formed (for example, it has no root). |
S_OK | 0 | The document is well-formed, has a DTD, and is valid according to that DTD |
Possible error messages returned with E_INVALID are listed below, together with the message ID used internally by MSXML:
Message ID | Message Returned by E_INVALID |
XML_ATTRIBUTE_FIXED | Attribute '%1' has a value that does not match the fixed value defined in the DTD/Schema. |
XML_ATTRIBUTE_NOT_DEFINED | The attribute '%1' on this element is not defined in the DTD/Schema. |
XML_ATTRIBUTE_VALUE | Attribute '%1' has an invalid value according to the DTD/Schema. |
XML_DTD_EXPECTING | Expecting: '%1'. |
XML_ELEMENT_ID_NOT_FOUND | The attribute '%1' references the ID '%2' which is not defined anywhere in the document. |
XML_ELEMENT_NOT_COMPLETE | Element content is incomplete according to the DTD/Schema. |
XML_ELEMENT_UNDECLARED | The element '%1' is used but not declared in the DTD/Schema. |
XML_ELEMENT_UNDEFINED | Reference to undeclared element: '%1'. |
XML_EMPTY_NOT_ALLOWED | Element cannot be empty according to the DTD/Schema. |
XML_ENTITY_UNDEFINED | Reference to undefined entity '%1'. |
XML_EXTENT_IN_ATTR | Cannot reference an external general parsed entity '%1' in an attribute value. |
XML_ILLEGAL_TEXT | Text is not allowed in this element according to DTD/Schema. |
XML_INFINITE_ENTITY_LOOP | Entity '%1' contains an infinite entity reference loop. |
XML_INVALID_CONTENT | Element content is invalid according to the DTD/Schema. |
XML_MULTI_FIXED_VALUES | An attribute declaration cannot contain multiple fixed values: '%1'. |
XML_NDATA_INVALID_REF | Cannot use unparsed entity '%1' in an entity reference. |
XML_NDATA_INVALID_PE | Cannot use the NDATA keyword in a parameter entity declaration. |
XML_REQUIRED_ATTRIBUTE_MISSING | Required attribute '%1' is missing. |
XML_REQUIRED_NDATA | Cannot use a general parsed entity '%1' as the value for attribute '%2'. |
XML_ROOT_NAME_MISMATCH | The name of the topmost element must match the name of the DOCTYPE declaration. |
XML_XMLNS_FIXED | Attribute '%1' must be a #FIXED attribute. |
Example
The following JScript example shows how to validate at runtime:
var xmldoc = new ActiveXObject("microsoft.xmldom");
xmldoc.async = false
//This will validate on load because validateOnParse is set to true
//by default.
xmldoc.load("http://server/myData.xml");
//You make a change to an attribute:
xmldoc.documentElement.setAttribute("a", "123");
//Now you want to verify your document is still valid:
try
{
(xmldoc.validate());
alert("Document is valid");
}
catch(e)
{
alert("Validation error:" + e.description);
}