Click here to Skip to main content
15,896,453 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello!

I have xml file and xsd file.
I want to validate xml file with xsd schema.
so, pls help me.
how to validate and read xml file.


best regards,
nwe nwe
Posted
Comments
krumia 16-Feb-12 3:11am    
Did you try typing this in google: "validate xml using xsd c#"

1 solution

Simple code to validate an XML file against a schema file (XSD)[^]

C#
using System.Xml;
using System.Xml.Schema;
using System.IO;

public class ValidXSD
{
    public static void Main()
    {

        // Set the validation settings.
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.ValidationType = ValidationType.Schema;
        settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
        settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
        settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
        settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);

        // Create the XmlReader object.
        XmlReader reader = XmlReader.Create("inlineSchema.xml", settings);

        // Parse the file. 
        while (reader.Read()) ;

    }
    // Display any warnings or errors.
    private static void ValidationCallBack(object sender, ValidationEventArgs args)
    {
        if (args.Severity == XmlSeverityType.Warning)
            Console.WriteLine("\tWarning: Matching schema not found.  No validation occurred." + args.Message);
        else
            Console.WriteLine("\tValidation error: " + args.Message);

    }
}


Or check this out.
http://msdn.microsoft.com/en-us/library/system.xml.schema.validationeventargs.severity.aspx[^]

Please vote and mark as solution if this helps you thanks.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900