Hi Guys,
seen this example on msdn, how can i remove the invalide nodes and elements when they fail against the schema and then save to a new xml document.
any help would be great
my code is below
thanks nathan
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(184, 32)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(80, 24)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Dim lineNumbers() As Integer
Dim lineCount As Integer = 0
Public Sub MyvalidationEventHandle(ByVal sender As Object, ByVal args As ValidationEventArgs)
Console.WriteLine(args.Message)
' I want to remove unwated tags and save the new xml doc with new name
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myschemacoll As New XmlSchemaCollection
Dim vr As XmlValidatingReader
Dim stream As FileStream
Dim LineIn As String
Dim LineInCount As Integer = 0
Dim iCount As Integer
Dim newXml As String
Try
stream = New FileStream("f:\nathan2.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
'Load the XmlValidatingReader.
vr = New XmlValidatingReader(stream, XmlNodeType.Element, Nothing)
'Add the schemas to the XmlSchemaCollection object.
myschemacoll.Add("", "f:\FFSN.xsd")
'myschemacoll.Add("urn:tapestore-schema", "c:\tape.xsd")
vr.Schemas.Add(myschemacoll)
vr.ValidationType = ValidationType.Schema
AddHandler vr.ValidationEventHandler, AddressOf MyvalidationEventHandle
While vr.Read()
End While
Dim i As Integer
For i = 0 To UBound(lineNumbers)
Console.WriteLine(lineNumbers(i))
Next
Console.WriteLine("Validation completed")
'This code catches any XML exceptions.
Catch XmlExp As XmlException
Console.WriteLine("Error trap 1 :" & vbCrLf & XmlExp.Message)
Console.WriteLine(XmlExp.LineNumber())
ReDim Preserve lineNumbers(lineCount)
lineNumbers(lineCount) = XmlExp.LineNumber()
lineCount = lineCount + 1
'This code catches any XML schema exceptions.
Catch XmlSchemaExp As XmlSchemaException
Console.WriteLine("Error trap 2 :" & vbCrLf & XmlSchemaExp.Message)
'This code catches any standard exceptions.
Catch GeneralExp As Exception
Console.WriteLine("Error trap 3 :" & vbCrLf & GeneralExp.Message)
Finally
'Clean up.
vr = Nothing
myschemacoll = Nothing
stream = Nothing
End Try
End Sub
end class