P
Praveen
Hi ,
I have an application which stores data as in XML. This data would get
updated at some intervals... I would be getting only update and added data
in an XML which I wanted to merge to the existing XML. I was reading about
the merge function of the dataset class and looks like that is exactly what
I need. I did some research and developed and XSD for the xml defenition. In
this XSD I have defined ID tag as the primary key. According to the article
found at
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcondiffgrams.asp
when there is a primary key in the xml schema the merge function would
update the destination dataset with the data from the source. Unfortunately
that did not happen for me. I was getting the below error.
An unhandled exception of type 'System.Data.ConstraintException' occurred in
system.data.dll
Additional information: Failed to enable constraints. One or more rows
contain values violating non-null, unique, or foreign-key constraints.
Also below is the code snippet, XML and XSD that I used. Can you advice me
what I am doing wrong.
Thanks in advance.
Praveen
DataSet DSSource = new DataSet();
DSSource.ReadXmlSchema(@"C:\custom.xsd");
DSSource.ReadXml(@"C:\Source.xml");
DataSet DSDEST = new DataSet();
DSDEST.ReadXmlSchema(@"C:\custom.xsd");
DSDEST.ReadXml(@"C:\Dest.xml");
DSDEST.Merge(DSSource,true,MissingSchemaAction.Add);
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Dest.xml
<ROOT>
<I><ID>2</ID><Name>John</Name></I>
<I><ID>5</ID><Name>Job</Name></I>
<I><ID>4</ID><Name>Jacob</Name></I>
<I><ID>6</ID><Name>Jimmy</Name></I>
</ROOT>
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Source.xml
<ROOT>
<I><ID>2</ID><Name>Mohit Mathew</Name></I>
<I><ID>3</ID><Name>Mac</Name></I>
</ROOT>
----------------------------------------------------------------------------------------------------------------------------------------------------------------
custom.xsd
<?xml version="1.0" standalone="yes" ?>
<xs:schema id="ROOT" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="ROOT" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="I">
<xs:complexType>
<xs:sequence>
<xs:element name="ID" type="xs:decimal" />
<xs:element name="Name" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:key name="ROOTKey1">
<xs:selector xpath=".//I" />
<xs:field xpath="ID" />
</xs:key>
</xs:element>
</xs:schema>
----------------------------------------------------------------------------------------------------------------------------------------------------------------
I have an application which stores data as in XML. This data would get
updated at some intervals... I would be getting only update and added data
in an XML which I wanted to merge to the existing XML. I was reading about
the merge function of the dataset class and looks like that is exactly what
I need. I did some research and developed and XSD for the xml defenition. In
this XSD I have defined ID tag as the primary key. According to the article
found at
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcondiffgrams.asp
when there is a primary key in the xml schema the merge function would
update the destination dataset with the data from the source. Unfortunately
that did not happen for me. I was getting the below error.
An unhandled exception of type 'System.Data.ConstraintException' occurred in
system.data.dll
Additional information: Failed to enable constraints. One or more rows
contain values violating non-null, unique, or foreign-key constraints.
Also below is the code snippet, XML and XSD that I used. Can you advice me
what I am doing wrong.
Thanks in advance.
Praveen
DataSet DSSource = new DataSet();
DSSource.ReadXmlSchema(@"C:\custom.xsd");
DSSource.ReadXml(@"C:\Source.xml");
DataSet DSDEST = new DataSet();
DSDEST.ReadXmlSchema(@"C:\custom.xsd");
DSDEST.ReadXml(@"C:\Dest.xml");
DSDEST.Merge(DSSource,true,MissingSchemaAction.Add);
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Dest.xml
<ROOT>
<I><ID>2</ID><Name>John</Name></I>
<I><ID>5</ID><Name>Job</Name></I>
<I><ID>4</ID><Name>Jacob</Name></I>
<I><ID>6</ID><Name>Jimmy</Name></I>
</ROOT>
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Source.xml
<ROOT>
<I><ID>2</ID><Name>Mohit Mathew</Name></I>
<I><ID>3</ID><Name>Mac</Name></I>
</ROOT>
----------------------------------------------------------------------------------------------------------------------------------------------------------------
custom.xsd
<?xml version="1.0" standalone="yes" ?>
<xs:schema id="ROOT" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="ROOT" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="I">
<xs:complexType>
<xs:sequence>
<xs:element name="ID" type="xs:decimal" />
<xs:element name="Name" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:key name="ROOTKey1">
<xs:selector xpath=".//I" />
<xs:field xpath="ID" />
</xs:key>
</xs:element>
</xs:schema>
----------------------------------------------------------------------------------------------------------------------------------------------------------------