If you want to return more than one validation error, then instead of boolean flag you 
need to create a collection and add each
validation error as they occur. A more robust implementation than the one I'm showing 
below would be to create your own Exception
type that has a string array of ValidationException messages as a member, and throw 
that when finished validating if there were any
exceptions. I typically put in some extra code to stop validating after a set number 
of validation errors because a simple error in
the beginning of a document can throw things off enough that you can get 100's of 
errors from a single document.

Sorry, but I don't have the cycles free to code up a robust example, so I'll just give 
you a down and dirty one that should get you
started. (Normal exception handling left out for clarity).

(Please forgive any errors. I'm throwing this down in Outlook)

public class1 {
  private ArrayList validationErrors = new ArrayList();
  private bool ignoreWarnings = false;

  public static void Main(string[] args) {
    //snipped code to create and initialize XmlValidatingReader (v)
    class1 c1 = new class1();
    ArrayList result = c1.ValidateDoc(v, false);

    for (int i=0;i<result.Count;++i) {
        Console.WriteLine("{0}", result[i]);
    }
  } //Main()

  public ArrayList ValidateDoc(XmlValidatingReader reader, bool ignoreWarnings) {
    this.validationErrors.Clear();
    this.ingoreWarnings = ignoreWarnings;

    reader.ValidationEventHandler += new 
ValidationEventHandler(this.reader_ValidationEvent);

    while reader.Read() {}

    reader.ValidationEventHandler -= new 
ValidationEventHandler(this.reader_ValidationEvent);
    return this.validationErrors;
  } //ValidateDoc()

  private void reader_ValidationEvent(object sender, ValidationEventArgs e) {
    if ((e.Severity == XmlSeverityType.Error) || (this.ignoreWarnings == false)) {
        this.validationErrors.Add(e.Message);
    }
  }

}//class1


Keep Smilin'
Ed Stegman

-----Original Message-----
From: Moderated discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED]]On Behalf Of Thaer
Sent: Sunday, February 16, 2003 7:58 AM
To: [EMAIL PROTECTED]
Subject: XML Validation by XSD


Hi,

                Iam a Java/C# developer, want to ask you about the
following:

                I create a sample to Validate XML file by XML Schema (XSD
file), Here is a snapshot from my code so as to let you know about my
problem:

            class Class1
          {
  static XmlTextReader r = null;
  static XmlValidatingReader v = null;
  private static bool isValid = true;      // If a validation
error occurs,
  public static void Main(string[] args)
  {
   r = new XmlTextReader(@"Product.xml");

              v = new XmlValidatingReader(r);
   v.ValidationType = ValidationType.Schema; // XSD
validation
   v.ValidationEventHandler += new
ValidationEventHandler(MyValidationEventHandler);

   while (v.Read())
   {
   }

   v.Close();

   // Check whether the document is valid or invalid.
   if (isValid)
   {
    Console.WriteLine("\n\nDocument is valid");
   }
   else
   {
    Console.WriteLine("\n\nDocument is
invalid");
   }
   Console.ReadLine();
  }
        public static void MyValidationEventHandler(object sender,
   ValidationEventArgs args)
  {
   isValid = false;
   Console.WriteLine("Validation event\n" +
args.Message);
  }
          }

Taking the above into consideration, when I enter a valid XML file with
Valid XSD file, the output will be

"Document is valid".

So every thing is good,
but when enter an InValid XML (i.e. the XML data has more than one error in
its data types according to XSD file), only the first error will be
reported by XML validator, so what is the right way to let the XML
validator to go through all the errors in XML and reporting them all.
Thanks.

Reply via email to