6

I'm working in a .net application where we need to generate XML files on the fly based on the dataset retrieved from the db. XML schema should be based on a xsd provided. I would like to know is there any way to bind or associate a dataset or each datarow with the xsd. I dont know whether it can be done at all or i may be thinking usage of XSDs at a wrong perspective. If i'm wrong please correct me and let me know of the best way to associate a data retrieved from db to a predefined schema.Thanks.

Update: If my perspective on xsd is wrong please shed some light on how xsds are used(or perhaps point me to some useful links).

1
  • I have been searching for such code, no readymade one available .. looks like it isn't that simple. Well. If it's for your testing then go for Altova XMLSpy, works like gem to create XMLs. Nov 22, 2012 at 15:30

2 Answers 2

3

Use the schema document as a parameter to the command line xsd.exe program included with visual studio to generate class files or typed datasets that you can include in your project/solution. These classes or datasets can be serialized to xml and will conform to the schema document you used to create them.

The only problem with this is that it's not dynamic: you can't wait until runtime to get the schema files. But there's nothing built in that supports this otherwise.

1

In addition to the solution suggested by Joel Coehoorn -- generate typed datasets or business entities from XSD -- let me add a couple of other approaches:

  1. If you use a database that supports XML type like Oracle or MS SQL Server, you can construct XML right in your SQL queries and retrieve XML directly from the database bypassing population of dataset.
  2. In case your database schema is not directly mapped to the given XSD, i.e. you already have a typed dataset or a set of XML-serializable business objects and those objects are serialized into XML that doesn't conform to XSD you're provided with, then you can use XSLT to transform your XML to another XML document which will be compliant with the given XSD.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.