📄 validating-xml.aspx
字号:
<%@Page Language="VB"%>
<%@Import Namespace="System.Xml" %>
<%@Import Namespace="System.Xml.Schema" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Validating XML documents with an XmlValidatingReader object</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Validating XML documents with an XmlValidatingReader object</span><hr />
<!--------------------------------------------------------------------------->
<form runat="server">
Select XML source document:
<ASP:DropDownList id="selXMLFile" runat="server">
<ASP:ListItem Text="A valid and well-formed XML document" Value="good-booklist.xml" />
<ASP:ListItem Text="A well-formed but invalid XML document" Value="bad-booklist.xml" />
<ASP:ListItem Text="An XML document that is invaild and not well-formed" Value="broken-booklist.xml" />
<ASP:ListItem Text="An XML document that doesn't even exist" Value="not-there.xml" />
</ASP:DropDownList>
<input type="submit" value="Go" /><p />
</form>
<div id="outXMLDoc" runat="server" /><p />
<script language="vb" runat="server">
'declare a variable to hold an XmlTextReader and one to hold the
'number of errors found. They have to be global because we need
'to access them within the event handler as well as Page_Load
Dim objXTReader As XmlTextReader
Dim intValidErrors As Integer = 0
Sub Page_Load()
'create physical path to booklist sample files (in same folder as ASPX page)
Dim strCurrentPath As String = Request.PhysicalPath
Dim strXMLPath As String = Left(strCurrentPath, InStrRev(strCurrentPath, "\")) & selXMLFile.SelectedItem.Value
Dim strSchemaPath As String = Left(strCurrentPath, InStrRev(strCurrentPath, "\")) & "booklist-schema.xsd"
'create the new XmlTextReader object and load the XML document
objXTReader = New XmlTextReader(strXMLPath)
outXMLDoc.innerHTML = "Loaded file: <b><a href=""" & strXMLPath _
& """>" & strXMLPath & "</a></b><br />"
'create an XMLValidatingReader for this XmlTextReader
Dim objValidator As New XmlValidatingReader(objXTReader)
'set the validation type to use an XSD schema
objValidator.ValidationType = ValidationType.Schema
'create a new XmlSchemaCollection
Dim objSchemaCol As New XmlSchemaCollection()
'add the booklist-schema.xsd schema to it
objSchemaCol.Add("", strSchemaPath)
'assign the schema collection to the XmlValidatingReader
objValidator.Schemas.Add(objSchemaCol)
outXMLDoc.innerHTML += "Validating against: <b><a href=""" & strSchemaPath _
& """>" & strSchemaPath & "</a></b><p />"
'add the event handler for any validation errors found
AddHandler objValidator.ValidationEventHandler, AddressOf ValidationError
Try
'iterate through the document using the contents as required
'we simply read each element here without using it for anything
While objValidator.Read()
'use or display the XML content here as required
End While
'display count of errors found
outXMLDoc.innerHTML += "<b>* Validation complete - " _
& intValidErrors & "</b> error(s) found"
Catch objError As Exception
'will occur if there is a read error or the document cannot be parsed
outXMLDoc.innerHTML += "<b>* Read/Parser error:</b> " & objError.Message & "<br />"
Finally
'must remember to always close the XmlTextReader after use
objXTReader.Close()
End Try
End Sub
'---------------------------------------------------------------------------
Public Sub ValidationError(objSender As Object, objArgs As ValidationEventArgs)
'event handler called when a validation error is found
intValidErrors += 1 'increment count of errors
'check the severity of the error
Dim strSeverity As String
If objArgs.Severity = 0 Then strSeverity = "Error"
If objArgs.Severity = 1 Then strSeverity = "Warning"
'display a message
outXMLDoc.innerHTML += "<b>* Validation error:</b> " & objArgs.Message _
& "<br /> Severity level: '<b>" & strSeverity & "</b>'. "
If objXTReader.LineNumber > 0 Then
outXMLDoc.innerHTML += "Line: " & objXTReader.LineNumber _
& ", character: " & objXTReader.LinePosition & "<br />"
End If
End Sub
</script>
<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -