Remove nodes when failed against xml schema

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nmaddock
    New Member
    • Feb 2008
    • 1

    Remove nodes when failed against xml schema

    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.Sche ma
    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.
    InitializeCompo nent()

    'Add any initialization after the InitializeCompo nent() 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.Disp ose()
    End If
    End If
    MyBase.Dispose( disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.Componen tModel.IContain er

    '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.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent()
    Me.Button1 = New System.Windows. Forms.Button
    Me.SuspendLayou t()
    '
    'Button1
    '
    Me.Button1.Loca tion = New System.Drawing. Point(184, 32)
    Me.Button1.Name = "Button1"
    Me.Button1.Size = New System.Drawing. Size(80, 24)
    Me.Button1.TabI ndex = 0
    Me.Button1.Text = "Button1"
    '
    'Form1
    '
    Me.AutoScaleBas eSize = 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 MyvalidationEve ntHandle(ByVal sender As Object, ByVal args As ValidationEvent Args)
    Console.WriteLi ne(args.Message )

    ' I want to remove unwated tags and save the new xml doc with new name

    End Sub

    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
    Dim myschemacoll As New XmlSchemaCollec tion
    Dim vr As XmlValidatingRe ader
    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.ReadW rite)
    'Load the XmlValidatingRe ader.
    vr = New XmlValidatingRe ader(stream, XmlNodeType.Ele ment, Nothing)

    'Add the schemas to the XmlSchemaCollec tion object.
    myschemacoll.Ad d("", "f:\FFSN.xs d")
    'myschemacoll.A dd("urn:tapesto re-schema", "c:\tape.xs d")
    vr.Schemas.Add( myschemacoll)
    vr.ValidationTy pe = ValidationType. Schema
    AddHandler vr.ValidationEv entHandler, AddressOf MyvalidationEve ntHandle

    While vr.Read()

    End While
    Dim i As Integer

    For i = 0 To UBound(lineNumb ers)
    Console.WriteLi ne(lineNumbers( i))

    Next
    Console.WriteLi ne("Validation completed")
    'This code catches any XML exceptions.
    Catch XmlExp As XmlException
    Console.WriteLi ne("Error trap 1 :" & vbCrLf & XmlExp.Message)


    Console.WriteLi ne(XmlExp.LineN umber())

    ReDim Preserve lineNumbers(lin eCount)
    lineNumbers(lin eCount) = XmlExp.LineNumb er()
    lineCount = lineCount + 1

    'This code catches any XML schema exceptions.
    Catch XmlSchemaExp As XmlSchemaExcept ion
    Console.WriteLi ne("Error trap 2 :" & vbCrLf & XmlSchemaExp.Me ssage)
    'This code catches any standard exceptions.
    Catch GeneralExp As Exception
    Console.WriteLi ne("Error trap 3 :" & vbCrLf & GeneralExp.Mess age)
    Finally
    'Clean up.
    vr = Nothing
    myschemacoll = Nothing
    stream = Nothing
    End Try
    End Sub
    end class
Working...