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 "EmployeesSchem a.xsd"
*************** *************** ***
<?xml version="1.0" encoding="utf-8" ?><xs:schema id="EmployeesSc hema" targetNamespace ="http://tempuri.org/EmployeesSchema .xsd" elementFormDefa ult="qualified
xmlns="http://tempuri.org/EmployeesSchema .xsd" xmlns:mstns="ht tp://tempuri.org/EmployeesSchema .xsd
xmlns:xs="http://www.w3.org/2001/XMLSchema"><xs: simpleType name="ZipCode"> <xs:restricti on base="xs:positi veInteger"><xs: pattern value="\d{5}" /></xs:restriction> </xs:simpleType>< xs:complexType name="Address"> <xs:sequence><x s: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="EmployeeL ist"><xs:comple xType><xs:seque nce><xs:element name="Employee" ><xs:complexTyp e><xs:sequence> <xs:element name="Email" type="xs:string " /><xs:element name="Password" type="xs:string " /><xs:element name="HomeAddre ss" type="Address" /><xs:element name="OtherAddr ess" 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" ?><EmployeeLis t 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_Clic k(object sender, System.EventArg s e
// Read the XML Schema into the DataSe
ds.ReadXmlSchem a(@"../../EmployeesSchema .xsd")
// Read the XML file into the DataSe
ds.ReadXml(@"../../Employees.xml")
// Bind the grid to the DataSe
dataGrid1.DataS ource = ds
private void btnDisplaySchem a_Click(object sender, System.EventArg s e
// Call the GetXmlSchema method to display the loaded schem
MessageBox.Show (ds.GetXmlSchem a())
private void btnGetXMLSchema _Click(object sender, System.EventArg s e
// Read the XML Schema into the DataSe
ds.ReadXmlSchem a(@"../../EmployeesSchema .xsd")
private void btnSaveXML_Clic k(object sender, System.EventArg s e
// Write out the data in the grid to a new XML fil
ds.WriteXml(@"N ewXmlFile.xml")
*************** **************c ode 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="EmployeeL ist" msdata:IsDataSe t="true" msdata:EnforceC onstraints="Fal se"
EmployeesSchema .xsd
<xs:element name="EmployeeL ist">
Schema from Dataset:
<xs:element name="EmployeeL ist" msdata:IsDataSe t="true" >
Also the Schema from Dataset and Employees.xsd:
<xs:simpleTyp e name="ZipCode"> <xs:restricti on base="xs:positi veInteger" /></xs:simpleType>
Whilst the EmployeesSchema .xsd is:
<xs:simpleTyp e name="ZipCode"> <xs:restricti on base="xs:positi veInteger"><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*******@disc ussions.microso ft.com> wrote in message
news:2D******** *************** ***********@mic rosoft.com...
Howto make datagrid enforce rules of xml schema?? Created xml schema in the designer. Constraints created there using the
following:<xs:simpleTyp e name="zipcode"> <xs:restricti on base="xs:string "><xs:patte rn
value="\d{5}" /></xs:restriction> </xs:simpleType>> > Datagrid does not enforce this rule, even though dataGrid1_Valid ating() is
called. dataset.WriteXm l() 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