Sign In/My Account | View Cart  
advertisement

Article:
 XML Data-Binding: Comparing Castor to .NET
Subject: validation in .Net?
Date: 2002-07-30 13:29:04
From: Takuki Kamiya

I could not get a clear idea as to where/when you can validate the XML being constructed in .NET, though it looks like it is handled entirely by unmarshaller because there is no setter method where early validation could have been provided.

Previous Message Previous Message   Next Message Next Message


Titles Only Titles Only Newest First
  • validation in .Net?
    2002-08-05 17:54:25 Niel Bornstein

    It's a fair question, and one which I did not address in this article.


    Basically, the validation of an XML document with a particular schema is done with XmlValidatingReader. One approach might be to create an instance of the DogShow class, then use XmlSerializer to write it to a MemoryStream. Then create an XmlValidatingReader, set its ValidationType to ValidationType.Schema, add the schema to its XmlSchemaCollection, and call Read().


    The following code snippet, replacing the XmlWriter portion of the C# sample, should provide some direction:


    Stream stream = new MemoryStream();
    TextWriter writer = new StreamWriter(stream);
    serializer.Serialize(writer,show);


    stream.Seek(0,SeekOrigin.Begin);


    XmlValidatingReader reader = new XmlValidatingReader(new XmlTextReader(stream));
    reader.ValidationType = ValidationType.Schema;
    reader.Schemas.Add("", "DogShow.xsd");


    while (reader.Read()) {
    // do nothing
    }


Sponsored By: