Solved

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

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
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 viewer will learn how to dynamically set the form action using jQuery.
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…

735 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