473,325 Members | 2,828 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,325 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 3608
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.