Validate XML against a DataSet Schema.  
Author Message

PostPosted: XML and the .NET Framework, Validate XML against a DataSet Schema. Top

I have limited XML skills and am completely baffled by how to validate an XMLDocument using a DataSet schema.

Let me explain, I have a web service with an XMLDocument as it's main input parameter.

This XML needs loading into my database. I can successfully do this as follows:-

oraCmd = new OracleCommand("MyOracleTable", oraConn);
oraCmd.CommandType = CommandType.TableDirect;
// get Data Adapter
oraDA = new OracleDataAdapter();
oraDA.SelectCommand = oraCmd;
// Create ins procs for saving data
OracleCommandBuilder oraCmdBuilder = new OracleCommandBuilder(oraDA);
oraDA.InsertCommand = oraCmdBuilder.GetInsertCommand();
// get schema for DataSet
DataSet oraDS = new DataSet();
oraDS.DataSetName = "Plan";
oraDA.FillSchema(oraDS, SchemaType.Source, "PlanDetails");
// Validate XML, using schema

// Read XML into Dataset
XmlTextReader r = new XmlTextReader(xd.OuterXml, XmlNodeType.Document, null);
// save to DB
int rowsAffected = oraDA.Update(oraDS, "PlanDetails");

So the problem comes when for example text has been entered into the XML element destined for a numeric field I get the error " Input string was not in a correct format". I would like to validate the XML using the table schema loaded into the DataSet, and produce a nice validation error list for feedback to the user, rather than it just falling over.

.NET Development17  
Martin Honnen

PostPosted: XML and the .NET Framework, Validate XML against a DataSet Schema. Top

What .NET version do you use, 2.0 If so and if xd is an XmlDocument instance then it has a method called Validate you can use to validate the in memory DOM document against one or more XSD schemas.

For .NET 1.x as you seem to serialize and reparse already (e.g. OuterXml, XmlTextReader) you could use an XmlValidatingReader.


PostPosted: XML and the .NET Framework, Validate XML against a DataSet Schema. Top

I'm using .Net 1.1.

I was looking at the XmlValidatingReader but I'm unsure of a lot of the related objects. I've got this so far but I'm not sure how to bring in the schema from my Dataset: -

XmlNamespaceManager nsmanager = new XmlNamespaceManager(myXmlDocument.NameTable);

XmlParserContext xmlPC = new XmlParserContext(myXmlDocument.NameTable, nsmanager, "", XmlSpace.None);

XmlValidatingReader validatingreader = new XmlValidatingReader(myXmlDocument.InnerXml, XmlNodeType.Element, xmlPC);

how do I get the schema from my DataSet into the below


How do I use the validatingreader

Thanks for any help.

Sergey Dubinets - MSFT

PostPosted: XML and the .NET Framework, Validate XML against a DataSet Schema. Top

For validation you need to have XSD.

XSD has nothing to do with DataSet and most likly you talk about some other schema.

As soon as you have a XSD and add it to ValidationReader you can read XML with this reader and reader would validate the XML while it reads it.


PostPosted: XML and the .NET Framework, Validate XML against a DataSet Schema. Top

Sergey thanks for your comments, not to be rude, but you are incorrect. The code in my first post retrieves the underlying database schema into the DataSet using FillSchema. A DataSet is interchangeable with it's XML definition, as is the schema. So the schema held in the Dataset is in fact a XSD.

The solution is to take the code from my first post and add the following where the is: -

XmlNamespaceManager nsmanager = new XmlNamespaceManager(xd.NameTable);
XmlParserContext pc =
new XmlParserContext(xd.NameTable, nsmanager, "", XmlSpace.None);
XmlTextReader tr = new XmlTextReader(xd.OuterXml, XmlNodeType.Element, pc);
XmlValidatingReader vr =
new XmlValidatingReader(tr);
XmlTextReader schematr = new XmlTextReader(oraDS.GetXmlSchema(), XmlNodeType.Document, pc);
vr.Schemas.Add("", schematr);
vr.ValidationType = ValidationType.Schema;
vr.ValidationEventHandler +=
new ValidationEventHandler (ValidationHandler);

Where xd is my XmlDocument , and add a procedure to collect all the errors something like this: -

public void ValidationHandler(object sender, ValidationEventArgs args)
this.errorMessage += args.Message + " ";