Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / February 2008

Tip: Looking for answers? Try searching our database.

On the Fly XML Validation

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Joy - 22 Feb 2008 19:02 GMT
Hi,
In my Project i am using a Charting tool called Fusion Charts. The charts
are rendered as Flash objects (.swf files) and it uses XML as the data source.

However in my Project the XML is generated at run time based on the search
criteria specified by the user, now my problem is before passiing the XML to
the chart object how do i verify the Well-formedness of the XML, so that
appropriate action could be taken before the user is presented with an error.

regards,
Joyjeet Nag
Martin Honnen - 23 Feb 2008 12:51 GMT
> In my Project i am using a Charting tool called Fusion Charts. The charts
> are rendered as Flash objects (.swf files) and it uses XML as the data source.
[quoted text clipped - 3 lines]
> the chart object how do i verify the Well-formedness of the XML, so that
> appropriate action could be taken before the user is presented with an error.

Well read through the XML with XmlReader, it checks for well-formedness
and throws an XmlException if there is an error.

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

Joy - 27 Feb 2008 14:26 GMT
Hi,
Can you send me a Sample code please?

And also i not only want to check the well-formedness of the XML but i would
also want to validate the XML against a XSD.

regards,
Joy

> > In my Project i am using a Charting tool called Fusion Charts. The charts
> > are rendered as Flash objects (.swf files) and it uses XML as the data source.
[quoted text clipped - 6 lines]
> Well read through the XML with XmlReader, it checks for well-formedness
> and throws an XmlException if there is an error.
Martin Honnen - 27 Feb 2008 15:33 GMT
> Can you send me a Sample code please?
>
> And also i not only want to check the well-formedness of the XML but i would
> also want to validate the XML against a XSD.

Do you have the XML in a string or as a file? Do you have the XSD schema
in a string or as a file?
Which language do you want to use, C# or VB.NET?

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

Joy - 28 Feb 2008 06:01 GMT
Hi,
My XML is in string however my XSD will be in a file.

I am using VB .NET.

regards,
Joy

> > Can you send me a Sample code please?
> >
[quoted text clipped - 4 lines]
> in a string or as a file?
> Which language do you want to use, C# or VB.NET?
Martin Honnen - 28 Feb 2008 13:04 GMT
> My XML is in string however my XSD will be in a file.
>
> I am using VB .NET.

Here is an example class that writes validation warning, validation
errors and well-formedness exceptions to the console:

Imports System
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO

Module TestValidation
    Sub Main()
        Dim xml As String = "<root><foo>1</foo></root>"
        Dim schema As String = "..\..\XSDSchema1.xsd"
        Dim validator As New ValidateXml(xml, schema)
        Dim valid As Boolean = validator.Validate()
        Console.WriteLine("{0} is valid: {1}.", xml, valid)

    End Sub
End Module

Public Class ValidateXml

    Private _valid As Boolean
    Public Property Valid() As Boolean
        Get
            Return _valid
        End Get
        Set(ByVal value As Boolean)
            _valid = value
        End Set
    End Property

    Private _xml As String
    Public Property Xml() As String
        Get
            Return _xml
        End Get
        Set(ByVal value As String)
            _xml = value
        End Set
    End Property

    Private _schemaFile As String
    Public Property SchemaFile() As String
        Get
            Return _schemaFile
        End Get
        Set(ByVal value As String)
            _schemaFile = value
        End Set
    End Property

    Public Sub New(ByVal x As String, ByVal schema As String)
        _xml = x
        _schemaFile = schema
    End Sub

    Public Function Validate() As Boolean
        _valid = True
        Dim settings As New XmlReaderSettings()
        settings.ValidationType = ValidationType.Schema
        settings.ValidationFlags = settings.ValidationFlags Or
XmlSchemaValidationFlags.ReportValidationWarnings
        AddHandler settings.ValidationEventHandler, AddressOf
ValidationHandler
        settings.Schemas.Add(Nothing, _schemaFile)
        Using reader As XmlReader = XmlReader.Create(New
StringReader(_xml), settings)
            Try
                While reader.Read()
                End While
            Catch ex As XmlException
                Console.WriteLine("Parse error: {0}", ex.Message)
                _valid = False
            End Try
            Return _valid
        End Using
    End Function

    Private Sub ValidationHandler(ByVal sender As Object, ByVal vargs
As ValidationEventArgs)
        If vargs.Severity = XmlSeverityType.Error Then
            _valid = False
        End If
        Console.WriteLine("{0}: {1}", vargs.Severity, vargs.Message)
    End Sub
End Class

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.