Solved

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

Posted on 2009-04-04
2
275 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 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 24070103
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…

813 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

12 Experts available now in Live!

Get 1:1 Help Now