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.XmlE xception - 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="Syst em.Xml.Schema" %>
<%@ Import Namespace="Syst em.Xml" %>
<HTML>
<script runat="server">
sub CheckXML(Sender as Object, e as EventArgs)
Dim sourceXML as String = Server.MapPath( xmlFileName.Tex t)
Dim sourceXSD as String = Server.MapPath( xsdFileName.Tex t)
errorMessage.Te xt = "Successful ly validated " & xmlFileName.Tex t & _
" using " & xsdFileName.Tex t
Dim validatingReade r as XmlValidatingRe ader
Dim xmlReader as XmlTextReader
Dim xsdReader as XmlTextReader
Dim testSchemaColle ction as new XmlSchemaCollec tion()
try
xsdReader = new XmlTextReader(s ourceXSD)
testSchemaColle ction.Add(xsdFi leName.Text, xsdReader)
xmlReader = new XmlTextReader(s ourceXML)
validatingReade r = new XmlValidatingRe ader(xmlReader)
validatingReade r.Schemas.Add(t estSchemaCollec tion)
validatingReade r.ValidationTyp e = ValidationType. Schema
AddHandler validatingReade r.ValidationEve ntHandler, _
AddressOf Me.ValidationCa llback
while (validatingRead er.Read())
end while
catch ex as Exception
errorMessage.Te xt = "XXX" & ex.ToString()
finally
if not xmlReader is nothing then xmlReader.Close ()
if not xsdReader is nothing then xsdReader.Close ()
if not validatingReade r is nothing then validatingReade r.Close()
end try
End Sub
sub ValidationCallb ack(sender as object, args as ValidationEvent Args)
if args.Severity = XmlSeverityType .Warning then
errorMessage.Te xt = "Warning: "
end if
if args.Severity = XmlSeverityType .Error then
errorMessage.Te xt = "Error: "
end if
if not args.Exception is nothing then
errorMessage.Te xt &= args.Message & " at line " & _
args.Exception. LineNumber & _
" position " & args.Exception. LinePosition & ControlChars.Cr Lf
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="CheckX ML" runat="server"
id="Button1" />
<br>
<asp:Label id="errorMessag e" runat="server" />
</form>
</font>
</body>
</HTML>
-----------------------------------------------------------
Here is the .XML file that I am using as input:
-----------------------------------------------------------
<?xml version="1.0" ?>
<computer xmlns="simple.x sd">
<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.x sd"
targetNamespace ="simple.xsd "
elementFormDefa ult="qualified" >
<xsd:element name="computer" >
<xsd:complexTyp e name="computerT ype">
<xsd:sequence >
<xsd:element name="name" type="xsd:strin g" />
<xsd:element name="os" type="xsd:strin g" />
<xsd:element name="speed" type="xsd:decim al" />
<xsd:element name="memory" type="xsd:decim al" />
</xsd:sequence>
</xsd:complexType >
</xsd:computer>
</xsd:schema>