By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,229 Members | 1,517 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,229 IT Pros & Developers. It's quick & easy.

Datagrid enforcing rules of xml schema?!

P: n/a
Howto make datagrid enforce rules of xml schema?

Created xml schema in the designer. Constraints created there using the following

<xs:simpleType name="zipcode"><xs:restriction base="xs:string"><xs:pattern value="\d{5}" /></xs:restriction></xs:simpleType

Datagrid does not enforce this rule, even though dataGrid1_Validating() is called.

dataset.WriteXml() saves withouth any warning, but

the following dataset.ReadXml() loads with error thrown

I am using the latest 2003 visual studio

Please if there is any working examples of this, or a somewhat extensive error report of dataset and datagrid in particular.

Rgds
O
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Frosty:

You're declaring/instantiating a strongly typed dataset or applying the
Schema and the grid is allowing it in ? Normally it just kicks out the
value to whatever it was originally if you put in something that violates
the rules. I've never seen this behavior --- can you show me the code that
makes the dataset (or the declaration of the STD) and the code you use to
bind it?
"Frosty" <an*******@discussions.microsoft.com> wrote in message
news:2D**********************************@microsof t.com...
Howto make datagrid enforce rules of xml schema??

Created xml schema in the designer. Constraints created there using the following:
<xs:simpleType name="zipcode"><xs:restriction base="xs:string"><xs:pattern value="\d{5}" /></xs:restriction></xs:simpleType>
Datagrid does not enforce this rule, even though dataGrid1_Validating() is called.
dataset.WriteXml() saves withouth any warning, but

the following dataset.ReadXml() loads with error thrown.

I am using the latest 2003 visual studio.

Please if there is any working examples of this, or a somewhat extensive error report of dataset and datagrid in particular.
Rgds,
OJ

Nov 16 '05 #2

P: n/a
Thanks for your reply William

I have made several attempts and all fails to enforce the constraint. Creating a strongly typed dataset using the designer, then adding (drag and drop) an instance of this dataset to a form together with a datagrid and setting up datagrids datamember and datasource properties in usual manner. No coding required this way, and all works fine except for enforcing the constraints. Have tried various constraints also but to no avail

My example for you follows a different approach. Starting with clean windows application, then adding two items "XML Schema" and "XML File", instead of the usual "Data Set" item, to the application. Then adding a datagrid and a few buttons to the form
*****************************************
***following is the code
*****************************************

***XML Schema, named "EmployeesSchema.xsd"
*********************************

<?xml version="1.0" encoding="utf-8" ?><xs:schema id="EmployeesSchema" targetNamespace="http://tempuri.org/EmployeesSchema.xsd" elementFormDefault="qualified
xmlns="http://tempuri.org/EmployeesSchema.xsd" xmlns:mstns="http://tempuri.org/EmployeesSchema.xsd
xmlns:xs="http://www.w3.org/2001/XMLSchema"><xs:simpleType name="ZipCode"><xs:restriction base="xs:positiveInteger"><xs:pattern value="\d{5}" /></xs:restriction></xs:simpleType><xs:complexType name="Address"><xs:sequence><xs:element name="Name" type="xs:string" /><xs:element name="Street" type="xs:string" /><xs:element name="State" type="xs:string" /><xs:element name="Zip" type="ZipCode" /></xs:sequence></xs:complexType><xs:element name="EmployeeList"><xs:complexType><xs:sequence>< xs:element name="Employee"><xs:complexType><xs:sequence><xs:e lement name="Email" type="xs:string" /><xs:element name="Password" type="xs:string" /><xs:element name="HomeAddress" type="Address" /><xs:element name="OtherAddress" type="Address" /></xs:sequence></xs:complexType></xs:element></xs:sequence></xs:complexType></xs:element></xs:schema

*************************************
***XML File named "Employees.xml"
***properties for this set to use the EmployeesSchema.xs
*********************************************

<?xml version="1.0" encoding="utf-8" ?><EmployeeList xmlns="http://tempuri.org/EmployeesSchema.xsd"></EmployeeList

************************************************** ***********
***cod
***in addition to defining dataset as part of form and creating in form constructor as usual
***private DataSet ds = new DataSet()
************************************************** ***********

private void btnLoadXML_Click(object sender, System.EventArgs e

// Read the XML Schema into the DataSe
ds.ReadXmlSchema(@"../../EmployeesSchema.xsd")

// Read the XML file into the DataSe
ds.ReadXml(@"../../Employees.xml")

// Bind the grid to the DataSe
dataGrid1.DataSource = ds

private void btnDisplaySchema_Click(object sender, System.EventArgs e

// Call the GetXmlSchema method to display the loaded schem
MessageBox.Show(ds.GetXmlSchema())
private void btnGetXMLSchema_Click(object sender, System.EventArgs e

// Read the XML Schema into the DataSe
ds.ReadXmlSchema(@"../../EmployeesSchema.xsd")
private void btnSaveXML_Click(object sender, System.EventArgs e

// Write out the data in the grid to a new XML fil
ds.WriteXml(@"NewXmlFile.xml")
*****************************code end****************************************

Still constraints are not enforced; ZipCode rejects non integer values as expected but does not enforce the /d{5} constraint.

The Employees.xml have the option of creating a schema file, Employees.xsd. I also added a display schema button that displays the xsd created from the dataset. Using this I have found the following which may indicate cause of error. I would expect the xsd files to be identical as they are all defined from EmployeesSchema.xsd, but they are not

Employees.xsd
<xs:element name="EmployeeList" msdata:IsDataSet="true" msdata:EnforceConstraints="False"

EmployeesSchema.xsd
<xs:element name="EmployeeList">

Schema from Dataset:
<xs:element name="EmployeeList" msdata:IsDataSet="true" >
Also the Schema from Dataset and Employees.xsd:
<xs:simpleType name="ZipCode"><xs:restriction base="xs:positiveInteger" /></xs:simpleType>

Whilst the EmployeesSchema.xsd is:

<xs:simpleType name="ZipCode"><xs:restriction base="xs:positiveInteger"><xs:pattern value="\d{5}" /></xs:restriction></xs:simpleType>

Rgds,
OJ

**********************************


----- William Ryan eMVP wrote: -----

Frosty:

You're declaring/instantiating a strongly typed dataset or applying the
Schema and the grid is allowing it in ? Normally it just kicks out the
value to whatever it was originally if you put in something that violates
the rules. I've never seen this behavior --- can you show me the code that
makes the dataset (or the declaration of the STD) and the code you use to
bind it?
"Frosty" <an*******@discussions.microsoft.com> wrote in message
news:2D**********************************@microsof t.com...
Howto make datagrid enforce rules of xml schema??
Created xml schema in the designer. Constraints created there using the following:<xs:simpleType name="zipcode"><xs:restriction base="xs:string"><xs:pattern value="\d{5}" /></xs:restriction></xs:simpleType>>> Datagrid does not enforce this rule, even though dataGrid1_Validating() is
called. dataset.WriteXml() saves withouth any warning, but
the following dataset.ReadXml() loads with error thrown.
I am using the latest 2003 visual studio.
Please if there is any working examples of this, or a somewhat extensive error report of dataset and datagrid in particular. Rgds,

OJ

Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.