本文整理匯總了VB.NET中System.Xml.Schema.XmlSchemaCollection.Add方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET XmlSchemaCollection.Add方法的具體用法?VB.NET XmlSchemaCollection.Add怎麽用?VB.NET XmlSchemaCollection.Add使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Xml.Schema.XmlSchemaCollection
的用法示例。
在下文中一共展示了XmlSchemaCollection.Add方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: SchemaCollectionSample
Option Strict
Option Explicit
Imports System.IO
Imports System.Xml
Imports System.Xml.Schema
Public Class SchemaCollectionSample
Private doc1 As String = "booksSchema.xml"
Private doc2 As String = "booksSchemaFail.xml"
Private doc3 As String = "newbooks.xml"
Private schema As String = "books.xsd"
Private schema1 As String = "schema1.xdr"
Private reader As XmlTextReader = Nothing
Private vreader As XmlValidatingReader = Nothing
Private m_success As Boolean = True
Public Sub New()
'Load the schema collection
Dim xsc As New XmlSchemaCollection()
xsc.Add("urn:bookstore-schema", schema) 'XSD schema
xsc.Add("urn:newbooks-schema", schema1) 'XDR schema
'Validate the files using schemas stored in the collection.
Validate(doc1, xsc) 'Should pass.
Validate(doc2, xsc) 'Should fail.
Validate(doc3, xsc) 'Should fail.
End Sub
Public Shared Sub Main()
Dim validation As New SchemaCollectionSample()
End Sub
Private Sub Validate(filename As String, xsc As XmlSchemaCollection)
m_success = True
Console.WriteLine()
Console.WriteLine("Validating XML file {0}...", filename.ToString())
reader = New XmlTextReader(filename)
'Create a validating reader.
vreader = New XmlValidatingReader(reader)
'Use the schemas stored in the schema collection.
vreader.Schemas.Add(xsc)
'Set the validation event handler.
AddHandler vreader.ValidationEventHandler, AddressOf ValidationCallBack
'Read and validate the XML data.
While vreader.Read()
End While
Console.WriteLine("Validation finished. Validation {0}", IIf(m_success, "successful", "failed"))
Console.WriteLine()
'Close the reader.
vreader.Close()
End Sub
Private Sub ValidationCallBack(sender As Object, args As ValidationEventArgs)
m_success = False
Console.Write((ControlChars.CrLf & ControlChars.Tab & "Validation error: " & args.Message))
End Sub
End Class
示例2: XmlSchemaCollection
Dim sc As XmlSchemaCollection = New XmlSchemaCollection()
AddHandler sc.ValidationEventHandler, AddressOf ValidationCallBack
' Create a resolver with the necessary credentials.
Dim resolver As XmlUrlResolver = New XmlUrlResolver()
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials
' Add the new schema to the collection.
sc.Add("", New XmlTextReader("sample.xsd"), resolver)