Option Explicit
Private Function HandleErrors()
MsgBox Err.Description
End Function
Private Sub Form_Resize()
' Resize text box to size of form.
Text1.Width = Form1.Width - 350
Text1.Height = Form1.Height - 750
End Sub
Private Sub Form_Load()
' Resize text box to size of form.
Text1 = ""
Text1.Top = 100
Text1.Left = 100
Text1.Width = Form1.Width - 350
Text1.Height = Form1.Height - 750
'C reate standalone SAX validator.
Dim vld As New MXValidator50
Dim cnth As MSXML2.IVBSAXContentHandler
Dim attrs As New SAXAttributes50
' Create the writer.
Dim wrt As New MXXMLWriter50
Set vld.errorHandler = wrt
' Create a schema collection to use in validation.
Dim Sch As New XMLSchemaCache50
' Load schemas into the schema collection, Sch.
Sch.Add "", App.Path & "\books.xsd"
vld.putProperty "schemas", Sch
Set cnth = vld
On Error GoTo errorHandler
' Configure the writer to indent elements.
wrt.indent = True
' Start the document.
cnth.startDocument
' Add the XML declaration.
cnth.processingInstruction "xml", "version='1.0'"
'Add the <catalog> element to the page.
cnth.startElement "", "catalog", "catalog", attrs
' Add the id attribute to the collection witht he "bk0101" value.
attrs.addAttribute "", "", "id", "CDATA", "bk101"
' Create the <book id="bk101"> tag.
cnth.startElement "", "book", "book", attrs
' Clear the attribute collection.
attrs.Clear
' Create the <author>Gambardella, Matthew</author> string.
cnth.startElement "", "author", "author", attrs
cnth.characters "Gambardella, Matthew"
cnth.endElement "", "author", "author"
' Create the <title>XML Developer's Guide</title> string.
cnth.startElement "", "title", "title", attrs
cnth.characters "XML Developer's Guide"
cnth.endElement "", "title", "title"
' Create the <genre>Computer</genre> string.
cnth.startElement "", "genre", "genre", attrs
cnth.characters "Computer"
cnth.endElement "", "genre", "genre"
' Create the <cost>44.95</cost> string.
' This element will be invalid according to books.xsd.
cnth.startElement "", "cost", "cost", attrs
cnth.characters "44.95"
cnth.endElement "", "cost", "cost"
' Create the <publish_date>2000-10-01</publish_date> string.
cnth.startElement "", "publish_date", "publish_date", attrs
cnth.characters "2000-10-01"
cnth.endElement "", "publish_date", "publish_date"
' Create the <description>An in-depth look at...</description> string.
cnth.startElement "", "description", "description", attrs
cnth.characters "An in-depth look at creating applications with XML"
cnth.endElement "", "description", "description"
' Add closing tags for the <book> and <catalog> elements.
cnth.endElement "", "book", "book"
cnth.endElement "", "catalog", "catalog"
' End the document.
cnth.endDocument
Text1.Text = "Success!" & vbCrLf & vbCrLf & wrt.output
Exit Sub
errorHandler:
Text1.Text = "Failure:" & vbCr & vbLf & wrt.output
End Sub
Try It!