Solved

i need to compare and validate my XML file with XSD file in windows forms using C#

Posted on 2009-04-04
2
262 Views
Last Modified: 2013-11-18
i need to compare and validate my XML file with XSD file in windows forms using C#
0
Comment
Question by:coventri
2 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
Here is a simple example which you can build upon.
using System;

using System.Xml;
 

namespace XML_Validate

{

    class Program

    {

        static bool errFound = false;
 

        static void Main(string[] args)

        {

            XmlDocument xdoc = new XmlDocument();

            XmlReaderSettings settings = new XmlReaderSettings();

            XmlReader reader;
 

            settings.ValidationType = ValidationType.Schema;

            settings.Schemas.Add(null, @"C:\test.xsd");

            settings.ValidationEventHandler += new System.Xml.Schema.ValidationEventHandler(validator_ValidationEventHandler);
 

            reader = XmlReader.Create(@"C:\test.xml", settings);
 

            try

            {

                xdoc.Load(reader);

            }

            catch (XmlException ex)

            {

                Console.WriteLine(ex.Message);

            }
 

            if (errFound)

            {

                Console.WriteLine("ERROR");

            }

            else

            {

                Console.WriteLine("No errors");

            }
 

            Console.ReadKey();

        }
 

        static void validator_ValidationEventHandler(object sender, System.Xml.Schema.ValidationEventArgs e)

        {

            errFound = true;

        }

    }

}

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The viewer will learn how to count occurrences of each item in an array.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now