The application should report that the XML document is valid.ġ0. V.Close()' Check whether the document is valid or invalid.Īfter the Main subroutine, write the MyValidationEventHandler subroutine, as follows: Public Sub MyValidationEventHandler(ByVal sender As Object, _Ĭonsole.WriteLine("Validation event" & vbCrLf & args.Message)
While v.Read()' Could add code here to process the content. You can check the status of isValid after validation to see whether the document is valid or invalid. This subroutine will set isValid to False (see step 8 of this section). If any validation errors occur, MyValidationEventHandler will be called to handle the error. Add the following code to register a validation event handler (you will implement the MyValidationEventHandler subroutine in step 8 of this section): AddHandler v.ValidationEventHandler, AddressOf MyValidationEventHandlerĪdd the following code to read and validate the XML document. If any validation errors occur, the validating reader generates a validation event. Set this property to DTD, as follows: v.ValidationType = ValidationType.DTD The XmlValidatingReader object has a ValidationType property, which indicates the type of validation required (DTD, XDR, or Schema). Then, create an XmlValidatingReader object to validate this XML data: Dim r As New XmlTextReader("C:\MyFolder\ProductWithDTD.xml") In the Main subroutine, create an XmlTextReader object to read an XML document from a text file. In Module1 (before the start of the Main subroutine), declare a boolean variable named isValid, as follows: 'If a validation error occurs, Imports ' For XmlSchemaCollection (used later) At the beginning of this file, add two Imports statements, as follows: Imports System.Xml ' For XmlTextReader and XmlValidatingReader NET displays a new file named Module1.vb.
NET, create a new Visual Basic Console Application project named ValidateXmlUsingVB. In Visual Studio 2005 or in Visual Studio. Save the modified XML document as ProductWithDTD.xml. This links the XML document to the DTD file). Add a DOCTYPE statement (below the ?xml version="1.0" line), as follows. NET to do this, point to Open on the File menu, and then click File. Reopen Product.xml in Visual Studio 2005 or in Visual Studio. Save the file as Product.dtd in the same folder as your XML document. Select the Text File type, and then click Open.Īdd the following DTD declarations to the file to describe the grammar of the XML document: NET, point to New on the File menu, and then click File. Save the file as Product.xml in a folder that you will be able to readily access later (the code samples in this article assume a folder named C:\MyFolder).Ĭreate a DTD and link to the XML document Select the XML File type, and then click Open.Īdd the following data to the XML document to represent a product in a catalog: Then, create a new XML file (on the File menu, point to New, and then click File). Start Microsoft Visual Studio 2005 or Microsoft Visual Studio.
You will also learn how to use the XmlSchemaCollection class to cache schemas in memory as a way to optimize XML validation. You will then learn how to use the XmlValidatingReader class to validate an XML document against the specified grammar. In this article, you will learn how to apply a DTD, an XDR schema, or an XSD schema to an XML document in Microsoft Visual Basic 2005 or in Microsoft Visual Basic. NET Framework, but DTDs and XDR schemas are also supported. XSD schemas are the preferred way to specify XML grammars in the. To specify the allowable structure and content of an XML document, you can write a Document Type Definition (DTD), a Microsoft XML-Data Reduced (XDR) schema, or an XML Schema definition language (XSD) schema. Original product version: Visual Basic Original KB number: 315533 SummaryĮxtensible Markup Language (XML) documents contain elements and attributes, and provide a flexible and powerful way to exchange data between applications and organizations. Also describes how to optimize validation by caching schemas.
This article shows how to use the XmlValidatingReader class to validate an XML document against a DTD, an XDR schema, or an XSD schema in Visual Basic 2005 or in Visual Basic.