473,387 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

xsd:restriction does it work with dataset.readxml ?

Mac
I'm trying to validate input from an xml source to a dataset in
dotnet2.0. As far as I can see, type errors correctly cause an
exception, but values that are the correct type but do not satisify
xsd:restriction conditions are ingested without a murmur. Is this a bug
or a feature?
Very frustrating.

Evidence:
1) XML data
<?xml version="1.0" encoding="utf-8"?>
<dataset xmlns="http://tempuri.org/DataSet1.xsd">
<datarow>
<c1>row1</c1>
<c2>5</c2>
</datarow>
<datarow>
<c1>row2</c1>
<c2>10</c2>
</datarow>
</dataset>

2) code fragment to read into a dataset (body of a button click
handler.
protected void Button1_Click(object sender, EventArgs e)
{
XmlReader rdr = new XmlTextReader(Server.MapPath(".\\" +
"XMLfile.xml"));
DataSet dsforload = new DataSet();
dsforload.ReadXmlSchema(Server.MapPath(".\\" +
"App_Code\\DataSet1.xsd"));
dsforload.ReadXml(rdr, XmlReadMode.Auto);
GridView1.DataSource = dsforload;
GridView1.DataBind();
}

3) complete xsd schema; quite a lot of random crap generated by the
'DataSet' wizard of VWD2005 - the interesting bit is at the bottom
xmlns="http://tempuri.org/DataSet1.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
xmlns:msprop="urn:schemas-microsoft-com:xml-msprop"
attributeFormDefault="qualified" elementFormDefault="qualified">
<xs:annotation>
<xs:appinfo source="urn:schemas-microsoft-com:xml-msdatasource">
<DataSource DefaultConnectionIndex="0"
FunctionsComponentName="QueriesTableAdapter"
Modifier="AutoLayout, AnsiClass, Class, Public"

xmlns="urn:schemas-microsoft-com:xml-msdatasource">
<Connections>
</Connections>
<Tables>
</Tables>
<Sources>
</Sources>
</DataSource>
</xs:appinfo>
</xs:annotation>
<xs:element name="dataset" msdata:IsDataSet="true"
msdata:UseCurrentLocale="true"
msprop:Generator_UserDSName="DataSet1"
msprop:Generator_DataSetName="DataSet1">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="datarow"
msprop:Generator_UserTableName="DataTable1"

msprop:Generator_RowDeletedName="DataTable1RowDele ted"
msprop:Generator_TableClassName="DataTable1DataTab le"
msprop:Generator_RowChangedName="DataTable1RowChan ged"
msprop:Generator_RowClassName="DataTable1Row"
msprop:Generator_RowChangingName="DataTable1RowCha nging"
msprop:Generator_RowEvArgName="DataTable1RowChange Event"

msprop:Generator_RowEvHandlerName="DataTable1RowCh angeEventHandler"
msprop:Generator_TablePropName="DataTable1"
msprop:Generator_TableVarName="tableDataTable1"
msprop:Generator_RowDeletingName="DataTable1RowDel eting">
<xs:complexType>
<xs:sequence>
<xs:element name="c1" type="xs:string" minOccurs="1"
></xs:element>
<xs:element name="c2" minOccurs="1" >
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:maxInclusive value="5"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

Aug 31 '06 #1
1 3611
Mac
I am an idiot - RTFM as they say in the Unix world - only its a case of
FTFM in this case as its rather obscure, imho.

In the original problem I erroneously thought that the schema was
partly validating the input because exceptions were thrown when the
schema datatype was wrong (eg data is a non-number and
.....type="xs:integer"...). What is actually happening here is that the
xsd is being used to type the columns of the dataset, and when you try
to assign something to a column of the wrong type you get a Format
error exception - nothing to do with xsd validation of the xml at all.

The solution, in a sentence, is that the XmlReader argument to
dataset.readxml() must have an XmlReaderSettings object associated with
it, with its ValidationType and Schemas properties set appropriately;
optionally you can set its validationeventhandler property as well. I
discovered all this from an excellent website I'd overlooked:

http://aspalliance.com/941#Page3 which I acknowledge with thanks.

Here is the revised code, which works. The only thing I haven't figured
out is how to pick up the Line Number and Line position from the
XmlReader - if anyone can see what the commented out statement in the
error handler is doing wrong I'd appreciate - its a scoping problem of
some kind.

protected void Button1_Click(object sender, EventArgs e)
{
StreamReader SR = new StreamReader(Server.MapPath(".\\" +
"App_Code\\DataSet1.xsd"));
XmlSchema Schema = new XmlSchema();
Schema = XmlSchema.Read(SR,
new
ValidationEventHandler(ReaderSettings_ValidationEv entHandler));
// 5- Create a new instance of XmlReaderSettings object
XmlReaderSettings ReaderSettings = new XmlReaderSettings();
// 6- Set ValidationType for XmlReaderSettings object
ReaderSettings.ValidationType = ValidationType.Schema;

// 7- Add Schema to XmlReaderSettings Schemas collection
ReaderSettings.Schemas.Add(Schema);
// 8- Add your ValidationEventHandler address to
// XmlReaderSettings ValidationEventHandler
// Leave out this step to use default error handler
ReaderSettings.ValidationEventHandler +=
new
ValidationEventHandler(ReaderSettings_ValidationEv entHandler);
XmlReader rdr = new XmlTextReader(Server.MapPath(".\\" +
"XMLfile.xml"));
XmlReader objrdr = XmlReader.Create(rdr,ReaderSettings);
DataSet dsforload = new DataSet();
dsforload.ReadXmlSchema(Server.MapPath(".\\" +
"App_Code\\DataSet1.xsd"));
dsforload.ReadXml(objrdr, XmlReadMode.Auto);
GridView1.DataSource = dsforload;
GridView1.DataBind();
}
private void ReaderSettings_ValidationEventHandler(object sender,
ValidationEventArgs args)
{
// 11- Implement your logic for each validation iteration
string strTemp;
//strTemp = "Line: " + this.objrdr.LineNumber + " - Position: "
//+ this.objrdr.LinePosition + " - " + args.Message;
strTemp = args.Message.ToString();
Label1.Text += strTemp;
}

Mac wrote:
I'm trying to validate input from an xml source to a dataset in
dotnet2.0. As far as I can see, type errors correctly cause an
exception, but values that are the correct type but do not satisify
xsd:restriction conditions are ingested without a murmur. Is this a bug
or a feature?
Very frustrating.

Evidence:
1) XML data
<?xml version="1.0" encoding="utf-8"?>
<dataset xmlns="http://tempuri.org/DataSet1.xsd">
<datarow>
<c1>row1</c1>
<c2>5</c2>
</datarow>
<datarow>
<c1>row2</c1>
<c2>10</c2>
</datarow>
</dataset>

2) code fragment to read into a dataset (body of a button click
handler.
protected void Button1_Click(object sender, EventArgs e)
{
XmlReader rdr = new XmlTextReader(Server.MapPath(".\\" +
"XMLfile.xml"));
DataSet dsforload = new DataSet();
dsforload.ReadXmlSchema(Server.MapPath(".\\" +
"App_Code\\DataSet1.xsd"));
dsforload.ReadXml(rdr, XmlReadMode.Auto);
GridView1.DataSource = dsforload;
GridView1.DataBind();
}

3) complete xsd schema; quite a lot of random crap generated by the
'DataSet' wizard of VWD2005 - the interesting bit is at the bottom
xmlns="http://tempuri.org/DataSet1.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
xmlns:msprop="urn:schemas-microsoft-com:xml-msprop"
attributeFormDefault="qualified" elementFormDefault="qualified">
<xs:annotation>
<xs:appinfo source="urn:schemas-microsoft-com:xml-msdatasource">
<DataSource DefaultConnectionIndex="0"
FunctionsComponentName="QueriesTableAdapter"
Modifier="AutoLayout, AnsiClass, Class, Public"

xmlns="urn:schemas-microsoft-com:xml-msdatasource">
<Connections>
</Connections>
<Tables>
</Tables>
<Sources>
</Sources>
</DataSource>
</xs:appinfo>
</xs:annotation>
<xs:element name="dataset" msdata:IsDataSet="true"
msdata:UseCurrentLocale="true"
msprop:Generator_UserDSName="DataSet1"
msprop:Generator_DataSetName="DataSet1">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="datarow"
msprop:Generator_UserTableName="DataTable1"

msprop:Generator_RowDeletedName="DataTable1RowDele ted"
msprop:Generator_TableClassName="DataTable1DataTab le"
msprop:Generator_RowChangedName="DataTable1RowChan ged"
msprop:Generator_RowClassName="DataTable1Row"
msprop:Generator_RowChangingName="DataTable1RowCha nging"
msprop:Generator_RowEvArgName="DataTable1RowChange Event"

msprop:Generator_RowEvHandlerName="DataTable1RowCh angeEventHandler"
msprop:Generator_TablePropName="DataTable1"
msprop:Generator_TableVarName="tableDataTable1"
msprop:Generator_RowDeletingName="DataTable1RowDel eting">
<xs:complexType>
<xs:sequence>
<xs:element name="c1" type="xs:string" minOccurs="1"
</xs:element>
<xs:element name="c2" minOccurs="1" >
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:maxInclusive value="5"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
Aug 31 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ed Slen | last post by:
Hi, Guys! Have a question about xsd restriction: I am having an element "state" and it could be US state (Which is easy) or Canadian postal code which is easy too (both rules are working...
0
by: SP | last post by:
Hi, I'm trying to write a correct xsd using restriction. I've the follow XML: <root> <a>10</a> <b>10,54</b> <c>2005-10-12</c> <d> <aa>test</aa> <bb>test2</bb>
1
by: MartinP | last post by:
Hi There. I've been doing a little research, but found little of value. My problem's this: I've got a DataSet containing a serious bunch of data. From this DataSet I want to "extract" some of...
0
by: Tim Nelson | last post by:
I just created an XSD file to map my XML data I want to load into the a dataset. Everything is rosy since all of my XML is basically elements with attributes. I can read the schema and bring in a...
0
by: # Cyrille37 # | last post by:
Hello, Printing a Dataset Schema Designer view is painfull. I got only bad result. I would like to know if there are applications that can open Dataset Schema Designer 's files ? Or is there...
1
by: Computer | last post by:
I brought an XSD into the XMLDesigner. The XSD had two related tables and it looked like this in .Net's XMLDesigner: Table 1 --------- BankItem (Bank Item)
1
by: Kevin Burton | last post by:
I have a set of XSD's that I would like to build some classes from. When I run the xsd tool I get a warning that the following is not "legal": <xsd:restriction base="xsd:string"> I have seen...
0
by: PJSLM | last post by:
I am having trouble using an AND operator and a not operator in XSD. I am trying to define a simple type that will allow 14 alphanumeric characters but most not contain the string ERR. I have tried...
2
by: Joris van Lier | last post by:
Hi All, i'm trying to define an xml schema for validation with a restriction on dateTime element based on another dateTime Element. in English: end (dateTime) should be greater than or equal to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.