Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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.
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.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

863 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

23 Experts available now in Live!

Get 1:1 Help Now