Home » Microsoft Technologies

Using an XML Schema to Create a DataSet and Load from Relational Database

I am attempting to create a Web service which returns a dataset (or even an XML document) that is valid against a particular schema. The schema only has a top element, say, Employees and multiple Employee elements inside which contain elements for name, email, telephone. ID is the only attribute to the Employee element. I am attempting to fill the dataset and return the dataset (or an XML document) to the client.

I have tried to create a dataset from the schema using the XmlReadSchema, and then create a dataadapter that fills the table in the dataset. During debugging, it appears that the datatable gets created, the dataadapter fetches the data but if I fill to the table created by the XmlReadSchema, it causes an error:


System.Data.ConstraintException: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.


If I look at the Constraints collection, the count = 0. If I fill a new table name "on the fly" (da.fill(ds, "DifferentTable")) it fills the dataset. I am returning a single row from the database in a table and with data specifically made to conform to the schema. I have done some reading, and I think a WCF solution is a direction I should go, but I need to provide solutions while I learn WCF.

What would be the best way to ensure that the data I return from an SQL Server table goes out the Web service formatted according to a particular schema?

(Note: I did review an article on Extending WebMethods with Schema Validation from a 2003 July article, but could not see the ValidationExtension type in the current .NET.)

Thank you for your advice.



3 Answers Found


Answer 1

Clearly you're having an ADO.NET problem right now, so I'll move your question to that forum.

I actually recommend against returning  a DataSet in any case. That is a type specific to .NET, so only a .NET client  will be able to consume it properly. In fact, there have been issues when the client and the service  are running different .NET versions.

Instead, you should return  a simple "data transfer object": an object that is all data  and no behavior. For instance:

public class GetEmployeesResponse
  public List<Employee> Employees {get;set;}

public class Employee
  public string ID {get;set;}
  public string FullName {get;set;}
  public string Email {get;set;}

Answer 2

Hi, John:

Thanks for your reply. I picked it up shortly after you posted, but then I began to research a DTO. The only references I could find were on how to do it with a Dataset. Plus, I've been busy. I see your solution  got marked as the answer, but I don't see from my references or from your example how this ensures that the returned "DTO" is compliant with my schema.

Do you have any good references from me to read about it?

I've also been trying to balance time with digging into WCF, but that still seems a bit distant right now.


Answer 3

Simply mark up the DTO class with the attributes necessary to force it to conform  to your schema  when it serializes.

If you're looking for something that references the schema and says, "go be like this", then you're not going to find it. The closest you can get is to take the schema(s) and run them through the xsd.exe program to produce classes which, when serialized will conform to the schema.

John Saunders
WCF is Web Services. They are not two separate things.
Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
Use File->New Project to create  Web Service Projects


<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure