Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries [Thread Prev] >Thread Next - Re: Problem Validating XML Document Problem Validating XML DocumentTo: NULL Date: 1/13/2009 3:12:00 PM Hello, I am creating an XML document in MSXML5 and attempting to validate it after I've completed it. Validating the document fails with an error code of -1072897500 and the message "The node is neither valid nor invalid because no DTD/Schema declaration was found." Oddly, if I save the XML document and read it back in again, it validates. Some sample code that produces this behavior is below, along with the xsd file. Can someone help resolve this issue? Regards, Martin Code: ' Initialize the XML instance document and the schema cache. Add the schema to the ' cache and assign it to the instance document. Dim xmlDocument As MSXML2.DOMDocument50 Set xmlDocument = New MSXML2.DOMDocument50 xmlDocument.async = False xmlDocument.validateOnParse = True xmlDocument.resolveExternals = True xmlDocument.preserveWhiteSpace = True Dim xmlSchemaCache As MSXML2.XMLSchemaCache50 Set xmlSchemaCache = New MSXML2.XMLSchemaCache50 xmlSchemaCache.Add "http://foo.com", "C:\Userdata\book.xsd" Set xmlDocument.Schemas = xmlSchemaCache ' Add the processing instruction. Dim xmlProc As IXMLDOMProcessingInstruction Set xmlProc = xmlDocument.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'") xmlDocument.appendChild xmlProc Set xmlProc = Nothing ' Create the collection root element with the namespace and schema attributes, and add it to the document. Dim elmCollection As MSXML2.IXMLDOMElement Set elmCollection = xmlDocument.createNode(NODE_ELEMENT, "b:Collection", "http://foo.com") Dim atrNSNode As IXMLDOMAttribute Set atrNSNode = xmlDocument.createNode(NODE_ATTRIBUTE, "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") atrNSNode.Value = "http://www.w3.org/2001/XMLSchema-instance" elmCollection.setAttributeNode atrNSNode Set atrNSNode = xmlDocument.createNode(NODE_ATTRIBUTE, "xsi:schemaLocation", "http://www.w3.org/2001/XMLSchema-instance") atrNSNode.Value = "http://foo.com C:\Userdata\book.xsd" elmCollection.setAttributeNode atrNSNode Set atrNSNode = Nothing xmlDocument.appendChild elmCollection ' Create the Book element and its children, and add it to the document. Dim elmBook As MSXML2.IXMLDOMElement Set elmBook = xmlDocument.createElement("b:Book") elmCollection.appendChild elmBook Dim elmTitle As IXMLDOMElement Dim elmAuthor As IXMLDOMElement Dim elmPublisher As IXMLDOMElement Set elmTitle = xmlDocument.createElement("b:Title") Set elmAuthor = xmlDocument.createElement("b:Author") Set elmPublisher = xmlDocument.createElement("b:Publisher") elmTitle.Text = "Lover Birds" elmAuthor.Text = "Cynthia Randall" elmPublisher.Text = "Lucerne Publishing" elmBook.appendChild elmTitle elmBook.appendChild elmAuthor elmBook.appendChild elmPublisher Set elmTitle = Nothing Set elmAuthor = Nothing Set elmPublisher = Nothing Set elmBook = Nothing ' Validate the document, write it to a file, then read it back in and validate again. Dim xmlParseError As MSXML2.IXMLDOMParseError Set xmlParseError = xmlDocument.validate ' <== Does not validate MsgBox "Error Code: " & xmlParseError.errorCode & vbNewLine & xmlParseError.reason Set xmlParseError = Nothing xmlDocument.Save "C:\Userdata\book.xml" Set xmlDocument = Nothing Set xmlDocument = New MSXML2.DOMDocument50 xmlDocument.Load ("C:\Userdata\book.xml") Set xmlParseError = xmlDocument.validate ' <== Validates successfully MsgBox "Error Code: " & xmlParseError.errorCode & vbNewLine & xmlParseError.reason Set xmlParseError = Nothing XSD: <?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://foo.com" elementFormDefault="qualified"> <xsd:element name="Collection"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
