?XML !doctype !element !element !element !element Note To /to From /from Heading /heading Body /body /note
?XML !doctype !element !element !element !element Note To /to From /from Heading /heading Body /body /note
?XML !doctype !element !element !element !element Note To /to From /from Heading /heading Body /body /note
Theory:
XML Schema:An XML Schema describes the structure of an XML document. The purpose
of an XML Schema is to define the legal building blocks of an XML document:
The XML Schema language is also referred to as XML Schema Definition (XSD).
DTD: A DTD is a Document Type Definition. A DTD defines the structure and the legal
elements and attributes of an XML document. With a DTD, independent groups of people
can agree on a standard DTD for interchanging data. An application can use a DTD to verify
that XML data is valid.
An Internal DTD Declaration: If the DTD is declared inside the XML file, it must be
wrapped inside the <!DOCTYPE> definition: XML document with an internal DTD
<?xml version="1.0"?>
<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend</body>
</note>
Department of Computer Science and Engineering (Data Science)
A.Y.: 2022-23 Class: S.Y.B.Tech. Sem: IV Sub: Web Engineering
In the XML file, select "view source" to view the DTD. The DTD is interpreted as follows:
!DOCTYPE note defines that the root element of this document is note
!ELEMENT note defines that the note element must contain four elements:
"to,from,heading,body"
!ELEMENT to defines the to element to be of type "#PCDATA"
!ELEMENT from defines the from element to be of type "#PCDATA"
!ELEMENT heading defines the heading element to be of type "#PCDATA"
!ELEMENT body defines the body element to be of type "#PCDATA"
If the DTD is declared in an external file, the <!DOCTYPE> definition must contain a
reference to the DTD file:
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
1. In Visual Studio 2005 or in Visual Studio .NET, create a new Visual Basic Console
Application project named ValidateXmlUsingVB.
2. Visual Studio 2005 or Visual Studio .NET displays a new file named Module1.vb. At
the beginning of this file, add two Imports statements, as follows:
vbnetCopy
Imports System.Xml ' For XmlTextReader and XmlValidatingReader
Imports System.Xml.Schema ' For XmlSchemaCollection (used later)
3. In Module1 (before the start of the Main subroutine), declare a boolean variable named
isValid, as follows:
vbnetCopy
'If a validation error occurs,
' set this flag to False
' in the validation event handler.
Private isValid As Boolean = True
4. In the Main subroutine, create an XmlTextReader object to read an XML document
from a text file. Then, create an XmlValidatingReader object to validate this XML data:
vbnetCopy
Dim r As New XmlTextReader("C:\MyFolder\ProductWithDTD.xml")
Dim v As New XmlValidatingReader(r)
Department of Computer Science and Engineering (Data Science)
A.Y.: 2022-23 Class: S.Y.B.Tech. Sem: IV Sub: Web Engineering
5. The XmlValidatingReader object has a ValidationType property, which indicates the
type of validation required (DTD, XDR, or Schema). Set this property to DTD, as
follows:
vbnetCopy
v.ValidationType = ValidationType.DTD
6. If any validation errors occur, the validating reader generates a validation event. Add
the following code to register a validation event handler :
vbnetCopy
AddHandler v.ValidationEventHandler, AddressOf MyValidationEventHandler
7. Add the following code to read and validate the XML document. If any validation
errors occur, MyValidationEventHandler will be called to handle the error. This
subroutine will set isValid to False (see step 8 of this section). Check the status of
isValid after validation to see whether the document is valid or invalid.
vbnetCopy
While v.Read()' Could add code here to process the content.
End While
v.Close()' Check whether the document is valid or invalid.
If isValid Then
Console.WriteLine("Document is valid")
Else
Console.WriteLine("Document is invalid")
End If
8. After the Main subroutine, write the MyValidationEventHandler subroutine, as
follows: vbnetCopy
Public Sub MyValidationEventHandler(ByVal sender As Object, _
ByVal args As ValidationEventArgs)
isValid = False
Console.WriteLine("Validation event" & vbCrLf & args.Message)
End Sub
9. Build and run the application.
The application should report that the XML document is valid.
10. In Visual Studio 2005 or in Visual Studio .NET, modify ProductWithDTD.xml to
make it invalid (for example, delete the ProductName Rugby
jersey/ ProductName element).
11. Run the application again.
The application should display the following error message:
Validation event Element 'Product' has incomplete content. Expected 'ProductName'.
An error occurred at file:///C:/MyFolder/ProductWithDTD.xml(4, 3). Document is
invalid
Store unstructured data in the SQL Server database
Most of the user-defined forms related platforms stores the XML template into the database
with XML data type and reference to that template user information will fill-up in the XML
document. A template will be the blank structure to the user for filling the details and filled
XML will be stored into the database as a user response.
Department of Computer Science and Engineering (Data Science)
A.Y.: 2022-23 Class: S.Y.B.Tech. Sem: IV Sub: Web Engineering
Advantages of XML data type in SQL Server:
The XML data type itself validates the input values while assigning value to the XML
column or variable. If something is wrong with the input, the query will return an error. For
example, I removed one closing tag (</title>) in the above XML, while assigning the value to
the variable. It will return an error “end tag does not match start tag” while assigning the
value to the variable: