Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using xsd to generate XML in .net

Tags:

.net

xml

xsd

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).

like image 817
lakshminb7 Avatar asked Sep 16 '08 19:09

lakshminb7


2 Answers

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.

like image 103
Joel Coehoorn Avatar answered Nov 04 '22 03:11

Joel Coehoorn


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.
like image 1
GregK Avatar answered Nov 04 '22 05:11

GregK