My message concerns a sample application that I took verbatim from the book
titled "SAMS Teach Yourself Visual Basic.NET Web Programming in 21 days".
It consists of a web page that takes the names of an XML file and
corresponding XML schema (XSD) file as input (via text boxes) and is
supposed to perform a
validation on the XML file. When I run it, I get the following error
message after entering the file names and clicking on the "Validate" button:
Run-time exception thrown : System.Xml.XmlException - The 'xsd:element'
start tag on line '7' doesn't match the end tag of 'xsd:schema' in file
'file:///C:/Inetpub/wwwroot/Practice/simple.xsd'. Line 17, position 3.
Can anyone explain why I get this error?
(I am getting errors also with other sample XML schema-related applications
with this book, so I may have follow-up messages.)
Bob Rosen
Here is the .aspx file that I am executing:
-----------------------------------------------------------
<%@ Page Language="vb" debug="true"%>
<%@ Import Namespace="System.Xml.Schema" %>
<%@ Import Namespace="System.Xml" %>
<HTML>
<script runat="server">
sub CheckXML(Sender as Object, e as EventArgs)
Dim sourceXML as String = Server.MapPath(xmlFileName.Text)
Dim sourceXSD as String = Server.MapPath(xsdFileName.Text)
errorMessage.Text = "Successfully validated " & xmlFileName.Text & _
" using " & xsdFileName.Text
Dim validatingReader as XmlValidatingReader
Dim xmlReader as XmlTextReader
Dim xsdReader as XmlTextReader
Dim testSchemaCollection as new XmlSchemaCollection()
try
xsdReader = new XmlTextReader(sourceXSD)
testSchemaCollection.Add(xsdFileName.Text, xsdReader)
xmlReader = new XmlTextReader(sourceXML)
validatingReader = new XmlValidatingReader(xmlReader)
validatingReader.Schemas.Add(testSchemaCollection)
validatingReader.ValidationType = ValidationType.Schema
AddHandler validatingReader.ValidationEventHandler, _
AddressOf Me.ValidationCallback
while (validatingReader.Read())
end while
catch ex as Exception
errorMessage.Text = "XXX" & ex.ToString()
finally
if not xmlReader is nothing then xmlReader.Close()
if not xsdReader is nothing then xsdReader.Close()
if not validatingReader is nothing then validatingReader.Close()
end try
End Sub
sub ValidationCallback(sender as object, args as ValidationEventArgs)
if args.Severity = XmlSeverityType.Warning then
errorMessage.Text = "Warning: "
end if
if args.Severity = XmlSeverityType.Error then
errorMessage.Text = "Error: "
end if
if not args.Exception is nothing then
errorMessage.Text &= args.Message & " at line " & _
args.Exception.LineNumber & _
" position " & args.Exception.LinePosition & ControlChars.CrLf
end if
End Sub
</script>
<body>
<font face="arial">
<form runat="server">
<h3>XML validation using XSD</h3>
Source XML File:
<asp:TextBox id="xmlFileName" runat="server" /><br>
Source XSD Schema:
<asp:TextBox id="xsdFileName" runat="server" /><br>
<asp:Button Text="Validate" OnClick="CheckXML" runat="server"
id="Button1" />
<br>
<asp:Label id="errorMessage" runat="server" />
</form>
</font>
</body>
</HTML>
-----------------------------------------------------------
Here is the .XML file that I am using as input:
-----------------------------------------------------------
<?xml version="1.0" ?>
<computer xmlns="simple.xsd">
<name>Bob's Computer</name>
<os>Windows XP</os>
<speed>800</speed>
<memory>256</memory>
</Computer>
-----------------------------------------------------------
and here is the .XSD file (simple.xsd) that I am using as input:
-----------------------------------------------------------
<?xml version="1.0" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="simple.xsd"
targetNamespace="simple.xsd"
elementFormDefault="qualified">
<xsd:element name="computer">
<xsd:complexType name="computerType">
<xsd:sequence>
<xsd:element name="name" type="xsd:string" />
<xsd:element name="os" type="xsd:string" />
<xsd:element name="speed" type="xsd:decimal" />
<xsd:element name="memory" type="xsd:decimal" />
</xsd:sequence>
</xsd:complexType>
</xsd:computer>
</xsd:schema>