Thanks for the response.
I'm already validating against the Schema using XmlValidatingReader but
that only gives the errors related to the presence/absence/ordering of
elements/attributes defined in the schema. I'll explain in the context of
the attached schema (BizScapeDataSet.xsd). The schema pertains to a DataSet
with one table BizScape. The primary key (Id) of this table is
AutoIncremented. I do the following in my code (see Code Block). When the
last line executes -- bizScapeDataSet.ReadXml(userXml) -- if there are two
BizScape elements in the Xml file with the same value for Id, then I get an
exception (Constraint violation).
What I would like to know is - which line in the Xml file is causing this
violation. This would be very useful when the Xml file is large and errors
have crept into it - such as duplicate id's.
Sudip
----------------------------- Code
Block --------------------------------------------------------
BizScapeDataSet bizScapeDataSet = new BizScapeDataSet();
// Read the BizScapeDataSet schema
XmlTextReader dataSetSchema =
SomeUtilityClass.ReadXml("BizScapeDataSet.xsd");
// Read in the Xml user data corresponding to the above schema
XmlTextReader userXml = new XmlTextReader("some user file name");
// Do schema validation
XmlSchemaCollection xmlSchemaCollection = new XmlSchemaCollection();
xmlSchemaCollection.Add(null, dataSetSchema);
XmlValidatingReader xmlValidatingReader = new XmlValidatingReader(userXml);
xmlValidatingReader.Schemas.Add(xmlSchemaCollectio n);
xmlValidatingReader.ValidationEventHandler +=
new ValidationEventHandler(this.OnValidationCallback);
while (xmlValidatingReader.Read()) {
}
// Load the Schema into the data set
bizScapeDataSet.ReadXmlSchema(dataSetSchema);
// Load the user data
bizScapeDataSet.ReadXml(userXml);
------------------------
BizScapeDataSet.xsd ----------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="BizScapeDataSet"
targetNamespace="http://voxvue.com/BizScapeDataSet.xsd"
elementFormDefault="qualified" attributeFormDefault="qualified"
xmlns="http://voxvue.com/BizScapeDataSet.xsd"
xmlns:bzs="http://voxvue.com/BizScapeDataSet.xsd"
xmlns:mstns="http://voxvue.com/BizScapeDataSet.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="BizScapeDataSet" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="BizScape" type="BizScape" maxOccurs="1" minOccurs="1" />
</xs:choice>
</xs:complexType>
<!-- Primary Keys -->
<xs:unique name="pk_bizscape_id" msdata:PrimaryKey="true">
<xs:selector xpath=".//BizScape" />
<xs:field xpath="Id" />
</xs:unique>
</xs:element>
<!-- Type Definitions -->
<xs:complexType name="BizScape">
<xs:sequence>
<xs:element name="Id" msdata:AutoIncrement="true" msdata:ReadOnly="true"
msdata:AutoIncrementSeed="1" type="xs:int" />
<xs:element name="Name" type="xs:string" msdata:AllowDBNull="false" />
</xs:sequence>
</xs:complexType>
<!-- Other Types -->
<!-- Relationships -->
</xs:schema>
"Oleg Tkachenko" <oleg@NO_SPAM_PLEASEtkachenko.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Sudip Chakraborty wrote:
Is there a way to see constraint validation errors while loading xml
into a DataSet ? I'm interested in the line number in the xml file which is
causing the error. I've enclosed the relevant stack trace below.
at System.Data.DataSet.FailedEnableConstraints()
at System.Data.DataSet.EnableConstraints()
at System.Data.DataSet.set_EnforceConstraints(Boolean value)
at System.Data.XmlDataLoader.LoadData(XmlReader reader)
at System.Data.DataSet.ReadXml(XmlReader reader)
Well, if you mean validation against XML Schema, use XmlValidatingReader
set on top of XmlReader. See "Validation of XML with
XmlValidatingReader" [1] for more info.
[1]
http://msdn.microsoft.com/library/de...tingreader.asp --
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel
"Oleg Tkachenko" <oleg@NO_SPAM_PLEASEtkachenko.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl... Sudip Chakraborty wrote:
Is there a way to see constraint validation errors while loading xml
into a DataSet ? I'm interested in the line number in the xml file which is
causing the error. I've enclosed the relevant stack trace below.
at System.Data.DataSet.FailedEnableConstraints()
at System.Data.DataSet.EnableConstraints()
at System.Data.DataSet.set_EnforceConstraints(Boolean value)
at System.Data.XmlDataLoader.LoadData(XmlReader reader)
at System.Data.DataSet.ReadXml(XmlReader reader)
Well, if you mean validation against XML Schema, use XmlValidatingReader
set on top of XmlReader. See "Validation of XML with
XmlValidatingReader" [1] for more info.
[1]
http://msdn.microsoft.com/library/de...tingreader.asp --
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel