VB.Net/XML/XML Schema — различия между версиями

Материал из VB Эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 12:42, 26 мая 2010

Compare the XML document generated from DataSet and Original one

Imports System
Imports System.Data
Module FidelityTester
    Sub Main()
        Dim MyDS As New DataSet()
        MyDS.ReadXmlSchema("book.xdr")
        MyDS.ReadXml("book.xml")
        MyDS.WriteXml("Book_After.xml")
    End Sub
End Module

"book.xdr
"<?xml version = "1.0"?>
"<Schema xmlns = "urn:schemas-microsoft-com:xml-data">
"   <ElementType name = "title" content = "textOnly" 
"      model = "closed" />
"   <ElementType name = "book" content = "eltOnly" model = "closed">
"      <element type = "title" minOccurs = "1" maxOccurs = "1" />
"   </ElementType>
"   <ElementType name = "books" content = "eltOnly" model = "closed">
"      <element type = "book" minOccurs = "0" maxOccurs = "*" />
"   </ElementType>
"</Schema>
"File: book.xml
"<?xml version = "1.0"?>
"<books xmlns = "x-schema:book.xdr">
"   <book>
"      <title>C# How to Program</title>
"   </book>
"   <book>
"      <title>Java How to Program, 4/e</title>
"   </book>
"   <book>
"      <title>Visual Basic .NET How to Program</title>
"   </book>
"   <book>
"      <title>Advanced Java 2 Platform How to Program</title>
"   </book>
"   <book>
"      <title>Python How to Program</title>
"   </book>
"</books>


Generate XML Schema from XML document

Imports System
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO
Imports System.Collections
Imports System.Data
Public Class MainClass
    
    Shared Sub Main()
      Dim nsArray As String() = {""}
      Dim MyDS As DataSet = New DataSet()
      MyDS.ReadXml("book.xml")
      MyDS.WriteXmlSchema("InferredSchema.xds")
    End Sub
End Class


Output data relation from XML document

Imports System
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO
Imports System.Collections
Imports System.Data
Public Class MainClass
    
    Shared Sub Main()
      Dim nsArray As String() = {""}
      Dim MyDS As DataSet = New DataSet()
      MyDS.ReadXml("book.xml")
      Console.WriteLine("Relation without schema:")
      Dim myRelation As DataRelation
      For Each myRelation In MyDS.Relations
         Console.WriteLine(myRelation.RelationName)
      Next
    End Sub
End Class


Read XML without schema and output its structure

Imports System
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Collections
Imports System.Data
Imports System.Windows.Forms

Public Class MainClass
    
    Shared Sub Main()
      Dim nsArray As String() = {""}
      Dim MyDS As DataSet = New DataSet()
      MyDS.ReadXml("book.xml")
      Console.WriteLine("Inferred Relational Structure:")
      Dim myTable As DataTable
      For Each myTable In MyDS.Tables
         Console.WriteLine("Table {0}", myTable.TableName)
         Dim myColumn As DataColumn
         For Each myColumn In myTable.Columns
            Console.WriteLine("  {0}", myColumn.ColumnName)
         Next
      Next
    End Sub
End Class
"<?xml version = "1.0"?>
"<books xmlns = "x-schema:book.xdr">
"   <book>
"      <title>C# How to Program</title>
"   </book>
"   <book>
"      <title>Java How to Program, 4/e</title>
"   </book>
"   <book>
"      <title>Visual Basic .NET How to Program</title>
"   </book>
"   <book>
"      <title>Advanced Java 2 Platform How to Program</title>
"   </book>
"   <book>
"      <title>Python How to Program</title>
"   </book>
"</books>


Validating XML documents against Schemas

Imports System.Xml
Imports System.Xml.Schema
Public Class MainClass
   Shared Dim valid As Boolean " validation result
   Shared Sub Main()
       Dim schemas As XmlSchemaCollection " Schemas
      valid = True " assume document is valid
      " get Schema(s) for validation
      schemas = New XmlSchemaCollection()
      schemas.Add("book", "book.xdr")
      " get XML document
      Dim reader As XmlTextReader = _
         New XmlTextReader("document.xml")
      " get validator
      Dim validator As XmlValidatingReader = _
         New XmlValidatingReader(reader)
      " assign Schema(s)
      validator.Schemas.Add(schemas)
      " Microsoft XDR validation
      validator.ValidationType = ValidationType.XDR
      " register event handler for validation error(s)
      AddHandler validator.ValidationEventHandler, _
         AddressOf ValidationError
      " validate document node-by-node
      While validator.Read
         " empty body
      End While
      " check validation result
      If valid Then
         Console.WriteLine("Document is valid")
      End If
      valid = True " reset variable
      validator.Close()
   End Sub 
   " event handler for validation error
   Shared Private Sub ValidationError(ByVal sender As Object, _
      ByVal arguments As ValidationEventArgs)
      Console.WriteLine(arguments.Message)
      valid = False " validation failed
   End Sub " ValidationError
End Class


XMl schema generator

Imports System
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Collections
Imports System.Data
Imports System.Windows.Forms

Public Class MainClass
    
    Shared Sub Main()
        Dim Schema As XmlSchema = New XmlSchema()
        Dim ElementBook As New XmlSchemaElement()
        Schema.Items.Add(ElementBook)
        ElementBook.Name = "Book"
        Dim ComplexType As New XmlSchemaComplexType()
        ElementBook.SchemaType = ComplexType
        Dim Sequence As New XmlSchemaSequence()
        ComplexType.Particle = Sequence
        Dim ElementTitle As New XmlSchemaElement()
        ElementTitle.Name = "Title"
        ElementTitle.SchemaTypeName = New XmlQualifiedName("string", _
                                       "http://www.w3.org/2001/XMLSchema")
        Dim ElementPublisher As New XmlSchemaElement()
        ElementPublisher.Name = "Publisher"
        ElementPublisher.SchemaTypeName = New XmlQualifiedName("string", _
                                       "http://www.w3.org/2001/XMLSchema")
        Sequence.Items.Add(ElementTitle)
        Sequence.Items.Add(ElementPublisher)
        Schema.rupile(New ValidationEventHandler(AddressOf ValidationHandler))
        Schema.Write(Console.Out)
    End Sub
    Shared Sub ValidationHandler(ByVal sender As Object, _
                          ByVal args As ValidationEventArgs)
        Console.WriteLine("Schema Validation Failed.")
        Console.WriteLine(args.Message)
    End Sub
End Class