Thanks Colbert
I setup a minimal console app to isolate this problem and it turns out that
if I read the dataset schema from file everything works
but if I read if from a stream it seems to just be ignored - not only the
maxLength property but the entire schema !
In the example below, reading the schema from file works as expected, the
dataset contains the table with the proper columns and column settings.
However after reading the schema from a stream the dataset is still empty -
so the dataset layout is determined solely by the contents of the xml which
is why the column settings from the schema are not having any effect.
All 3 versions of the schema give the same result ( the 1st one is mine ,
the second one is from an vs dataset designer WriteXmlSchema , the third is
the designer schema with a restriction defined. )
Any idea how we can get this to work when reading the schema from a stream -
is there a XmlReaderSettings property that I am not seeing ?
Gerry
using System;
using System.Data;
using System.IO;
using System.Reflection;
using System.Xml.Schema;
using System.Xml;
namespace Dbg
{
class Program
{
static void Main( string[] args )
{
string xsdPath = "c:\\tmp\\test.xsd";
string xmlPath = "c:\\tmp\\test.xml;
//-------------------------------------------------
// save dataset designer schema to file
//-------------------------------------------------
test ds1 = new test();
// ds1.WriteXmlSchema( xsdPath );
DumpDataSet( ds1 , "ds1" );
//-------------------------------------------------
// READ SCHEMA FROM FILE
// this dumps everything as expected
//-------------------------------------------------
DataSet ds2 = new DataSet();
ds2.ReadXmlSchema( xsdPath );
DumpDataSet( ds2 , "ds2" );
//-------------------------------------------------
// READ SCHEMA FROM STREAM
// without loading xml doc this dumps nothing
// if xml doc loaded, dumps expected tables/columns based on xml
parse - ie. maxLength = -1
//-------------------------------------------------
using ( XmlReader xsdReader = new XmlTextReader( "file:\\" +
xsdPath ) )
{
XmlSchema xsdSchema = XmlSchema.Read( xsdReader , null );
using ( DataSet ds3 = new DataSet() )
{
ds3 .ReadXmlSchema( xsdReader );
DumpDataSet( ds3 , "ds3" );
}
}
}
static void DumpDataSet( DataSet ds , string name )
{
Console.WriteLine( "\nDump {0}" , name );
foreach ( DataTable table in ds.Tables )
{
Console.WriteLine( "Table : {0}" , table.TableName );
foreach ( DataColumn column in table.Columns )
Console.WriteLine( " : {0} {1}" , column.ColumnName ,
column.MaxLength );
}
}
static void XmlValidationEventHandler( object sender ,
ValidationEventArgs e )
{
throw new Exception( string.Format( "Xml validation error @ line {0}
position {1} : {2}" ,
e.Exception.LineNumber , e.Exception.LinePosition ,
e.Message ) ,
e.Exception );
}
}
}
<xs:schema xmlns:xs="
http://www.w3.org/2001/XMLSchema"
attributeFormDefault="qualified" elementFormDefault="qualified">
<xs:simpleType name="s10">
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="10"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="table" >
<xs:complexType>
<xs:sequence>
<xs:element name="fld1" minOccurs="0" type="s10" />
<xs:element name="fld2" minOccurs="0" type="s10" />
<xs:element name="fld3" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<?xml version="1.0" standalone="yes"?>
<xs:schema id="test" targetNamespace="
http://tempuri.org/test.xsd"
xmlns:mstns="
http://tempuri.org/test.xsd" xmlns=
http://tempuri.org/test.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:element name="test" msdata:IsDataSet="true"
msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="test" msprop:Generator_UserTableName="response"
msprop:Generator_TablePropName="_test">
<xs:complexType>
<xs:sequence>
<xs:element name="fld1" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="10" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="fld2" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="10" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="fld3" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<?xml version="1.0" standalone="yes"?>
<xs:schema id="test" targetNamespace="
http://tempuri.org/test.xsd"
xmlns:mstns="
http://tempuri.org/test.xsd" xmlns=
http://tempuri.org/test.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:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="10"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="test" msdata:IsDataSet="true"
msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="test" msprop:Generator_UserTableName="response"
msprop:Generator_TablePropName="_test">
<xs:complexType>
<xs:sequence>
<xs:element name="fld1" minOccurs="0" type="s10" />
<xs:element name="fld2" minOccurs="0" type="s10" />
<xs:element name="fld3" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<?xml version='1.0' standalone='yes'?>
<table>
<fld1>fld1</fld1>
<fld2>fld2</fld2>
<fld3>fld3</fld3>
</table>