473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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.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
Feb 26 '08 #1
0 1041

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: indo3 | last post by:
Hello, I program an xml editor with xerces for the university of Braunschweig, the user dont see any xml tags, it is represented in a JTree. Now the user shall be able to insert nodes, which...
2
by: Claudio Jolowicz | last post by:
How can XSLT stylesheets be used to edit, remove and add nodes specified by their position in the document tree? The XML document stores development tasks in a hierarchical way, i.e. tasks can...
2
by: RobG | last post by:
Why does Firefox insert #text nodes as children of TR elements? As a work-around for older Safari versions not properly supporting a table row's cells collection, I used the row's childNodes...
1
by: eXavier | last post by:
Hi, I need to validate XML fragment against XSD schema. The main issue is that xml fragment does not contain refrence to schema, but I want to force the validation against the schema I have in...
0
by: Nick Wong | last post by:
I am using a XmlValidatingReader to validate an xml against a xsd schema. What I would like to achieve is to "mark" each invalid node element with an attribute. The resultant xml is then passed...
2
by: fizzy | last post by:
i am fetching an xml document with the following structure: <?xml version="1.0" encoding="UTF-8"?> <DTCResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"...
80
by: Andrew R | last post by:
Hi I'm creating a series of forms, each with with around 15-20 text boxes. The text boxes will show data from tables, but are unbound to make them more flexible. I want the form to be used...
3
by: dkacher | last post by:
Hi - I'm looking for a way to generate a list of the fully-qualified paths to all of the leaf nodes in an XML Schema. The reason: I have a large schema for which I'm building a transform...
4
by: agda.karlberg | last post by:
Hello, I need to remove the DTD reference from an xml document, the reason for this is that we want to validate against a schema instead (which we have locally). It takes up to a minute to fetch...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.