none
XmlReadMode.IgnoreSchema - did it discard data? RRS feed

  • Question

  • Hi all,

    I fill a strongly-typed dataset from an XML file using DataSet.ReadXML(String, XmlReadMode.IgnoreSchema).

    Is there a way to tell whether data in the XML file was discarded (as per the IgnoreSchema enumeration - "If any data does not match the existing schema, it is discarded")?

    Some detail: my application has three strongly-typed datasets defined. The user should choose which dataset to fill (via one of three radio buttons) and then chose an "matching" XML data file.

    I want to trap if the user has mismatched their selection of dataset and data file, and I was hoping that there would be some return code or exception raised by Dataset.ReadXml if it was unable to successfully load all the data (ie, some was discarded). Instead, ReadXml returns the same XmlReadMode as is specified in the method parameters, for which I can't see a use.

    Is there a way to determine whether ReadXml successfully got all the data?

    Thanks

    Matt



    Wednesday, March 12, 2008 10:33 AM

Answers

  • I do not think DataSet validates data and throws an exception on it's own. However to insert data to DataSet you can use a reader which validates data against the schema. Take a look at the example:

     

    Code Snippet

    private static void ValidateDataSetInput()

    {

    DataSet newDataSet = new DataSet("New DataSet");

    newDataSet.ReadXmlSchema("schema.xsd");

     

    XmlSchemaSet schemaSet = new XmlSchemaSet();

    XmlSchema schema = new XmlSchema();

    schemaSet.Add(null, "schema.xsd");

     

    XmlReaderSettings readerSettings = new XmlReaderSettings();

    readerSettings.ValidationType = ValidationType.Schema;

    readerSettings.Schemas = schemaSet;

    // you can add validation event handler

    //readerSettings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);

     

    using(XmlReader xr = XmlReader.Create("XmlDocument.xml", readerSettings))

    {

    newDataSet.ReadXml(xr);

    }

    }

     

    private static void ValidationCallBack(object sender, ValidationEventArgs e)

    {

    Console.WriteLine("Validation Error: {0}", e.Message);

    }

     

     

     

    In the example I added some additional settings to the XmlReader that forces the reader to validate the input. If you need to be more flexible you can add validation call back which will be executed for each validation error. If the validation callback is not provided an exception will be thrown.

     

    Hope this helps

    Pawel

    Wednesday, March 12, 2008 5:19 PM

All replies

  • I do not think DataSet validates data and throws an exception on it's own. However to insert data to DataSet you can use a reader which validates data against the schema. Take a look at the example:

     

    Code Snippet

    private static void ValidateDataSetInput()

    {

    DataSet newDataSet = new DataSet("New DataSet");

    newDataSet.ReadXmlSchema("schema.xsd");

     

    XmlSchemaSet schemaSet = new XmlSchemaSet();

    XmlSchema schema = new XmlSchema();

    schemaSet.Add(null, "schema.xsd");

     

    XmlReaderSettings readerSettings = new XmlReaderSettings();

    readerSettings.ValidationType = ValidationType.Schema;

    readerSettings.Schemas = schemaSet;

    // you can add validation event handler

    //readerSettings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);

     

    using(XmlReader xr = XmlReader.Create("XmlDocument.xml", readerSettings))

    {

    newDataSet.ReadXml(xr);

    }

    }

     

    private static void ValidationCallBack(object sender, ValidationEventArgs e)

    {

    Console.WriteLine("Validation Error: {0}", e.Message);

    }

     

     

     

    In the example I added some additional settings to the XmlReader that forces the reader to validate the input. If you need to be more flexible you can add validation call back which will be executed for each validation error. If the validation callback is not provided an exception will be thrown.

     

    Hope this helps

    Pawel

    Wednesday, March 12, 2008 5:19 PM
  • Thanks Pawel, this did help. My XSD files were a bit troublesome to match against the incoming XML but it's sorted now.

     

     

    Thursday, March 13, 2008 5:19 PM