×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

more sample in VB.net

本文发表在 rolia.net 枫下论坛Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Schema

namespace HowTo.Samples.XML

public class SchemaCollectionSample

private const xmlDocument as String = "xmlsc.xml"
private const xsdDocument as String = "xmlsc.xsd"

public shared sub Main()
Dim args As String() = {xmlDocument, xsdDocument}
Dim mySchemaCollectionSample as SchemaCollectionSample = new SchemaCollectionSample()
mySchemaCollectionSample.Run(args)
end sub

public sub Run(args As String())

Dim reader as XmlValidatingReader
Dim xsc as XmlSchemaCollection = new XmlSchemaCollection()

try
xsc.Add(xsdDocument, new XmlTextReader(args(1)))
reader = new XmlValidatingReader(new XmlTextReader(args(0)))

' Set the validation event handler
Dim valdel as ValidationEventHandler = new ValidationEventHandler(addressof ValidationEvent)

AddHandler reader.ValidationEventHandler, valdel
reader.Schemas.Add(xsc)
reader.ValidationType = ValidationType.Schema

while (reader.Read())
Dim i as Integer
For i=0 to (reader.Depth - 1)
Console.Write(Chr(9))
Next
Console.WriteLine ("{0}<{1}>{2}", reader.NodeType, reader.Name, reader.Value)

if (reader.NodeType = XmlNodeType.Element)
while (reader.MoveToNextAttribute())
For i=0 to (reader.Depth - 1)
Console.Write(Chr(9))
Next
Console.WriteLine ("{0}<{1}>{2}", reader.NodeType, reader.Name, reader.Value)
end while
end if
end while
catch e as Exception
Console.WriteLine(e.ToString())
finally
if (Not reader is Nothing)
reader.Close()
end if
end try
end sub

public sub ValidationEvent (errorid as object, args as ValidationEventArgs)

Console.WriteLine(Strings.chr(9) & "Validation error: " & args.Message)

if (args.Severity = XmlSeverityType.Warning)
Console.WriteLine("No schema found to enforce validation.")
elseif (args.Severity = XmlSeverityType.Error)
Console.WriteLine("validation error occurred when validating the instance document.")
end if

if Not (args.Exception is Nothing) ' XSD schema validation error
Console.WriteLine(args.Exception.SourceUri & "," & args.Exception.LinePosition & "," & args.Exception.LineNumber)
end if

'if (myXmlValidatingReader.Reader.LineNumber > 0)
' Console.WriteLine("Line: " & myXmlValidatingReader.Reader.LineNumber & " Position: " & myXmlValidatingReader.Reader.LinePosition)
'end if
end sub

end Class
end Namespace更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / 向高手请教:有没有这样的.net 源代码例子:用来判断一个xml 文件是否符合一定的xml schema? 谢谢先。
    • parse 一下不好么?
      • 我要自己写,找到一个,先试试吧。
    • inside
      XmlTextReader tr = new XmlTextReader("Sample1.xml");
      XmlValidatingReader vr = new XmlValidatingReader(tr);
      • Thanks. :)
      • 问题:
        我用了如下办法:
        Dim xsc As XmlSchemaCollection = New XmlSchemaCollection
        xsc.Add("generic", schemaFile)
        运行到第二句出现错误:
        An unhandled exception of type 'System.Xml.Schema.XmlSchemaException' occurred in system.xml.dll

        Additional information: System error.
        怎么办啊。
        :(
        谢谢
        • more sample in VB.net
          本文发表在 rolia.net 枫下论坛Imports Microsoft.VisualBasic
          Imports System
          Imports System.IO
          Imports System.Xml
          Imports System.Xml.Schema

          namespace HowTo.Samples.XML

          public class SchemaCollectionSample

          private const xmlDocument as String = "xmlsc.xml"
          private const xsdDocument as String = "xmlsc.xsd"

          public shared sub Main()
          Dim args As String() = {xmlDocument, xsdDocument}
          Dim mySchemaCollectionSample as SchemaCollectionSample = new SchemaCollectionSample()
          mySchemaCollectionSample.Run(args)
          end sub

          public sub Run(args As String())

          Dim reader as XmlValidatingReader
          Dim xsc as XmlSchemaCollection = new XmlSchemaCollection()

          try
          xsc.Add(xsdDocument, new XmlTextReader(args(1)))
          reader = new XmlValidatingReader(new XmlTextReader(args(0)))

          ' Set the validation event handler
          Dim valdel as ValidationEventHandler = new ValidationEventHandler(addressof ValidationEvent)

          AddHandler reader.ValidationEventHandler, valdel
          reader.Schemas.Add(xsc)
          reader.ValidationType = ValidationType.Schema

          while (reader.Read())
          Dim i as Integer
          For i=0 to (reader.Depth - 1)
          Console.Write(Chr(9))
          Next
          Console.WriteLine ("{0}<{1}>{2}", reader.NodeType, reader.Name, reader.Value)

          if (reader.NodeType = XmlNodeType.Element)
          while (reader.MoveToNextAttribute())
          For i=0 to (reader.Depth - 1)
          Console.Write(Chr(9))
          Next
          Console.WriteLine ("{0}<{1}>{2}", reader.NodeType, reader.Name, reader.Value)
          end while
          end if
          end while
          catch e as Exception
          Console.WriteLine(e.ToString())
          finally
          if (Not reader is Nothing)
          reader.Close()
          end if
          end try
          end sub

          public sub ValidationEvent (errorid as object, args as ValidationEventArgs)

          Console.WriteLine(Strings.chr(9) & "Validation error: " & args.Message)

          if (args.Severity = XmlSeverityType.Warning)
          Console.WriteLine("No schema found to enforce validation.")
          elseif (args.Severity = XmlSeverityType.Error)
          Console.WriteLine("validation error occurred when validating the instance document.")
          end if

          if Not (args.Exception is Nothing) ' XSD schema validation error
          Console.WriteLine(args.Exception.SourceUri & "," & args.Exception.LinePosition & "," & args.Exception.LineNumber)
          end if

          'if (myXmlValidatingReader.Reader.LineNumber > 0)
          ' Console.WriteLine("Line: " & myXmlValidatingReader.Reader.LineNumber & " Position: " & myXmlValidatingReader.Reader.LinePosition)
          'end if
          end sub

          end Class
          end Namespace更多精彩文章及讨论,请光临枫下论坛 rolia.net
        • here is the URL
          • 谢谢。。。。