views:

366

answers:

2

Hello there, I have been thinking about the optimal way to create an XML file using data from a Dataset AND according to the rules of an XML schema.

I've been searching around for a bit, and I failed to find a way in which I only take the data from the Dataset and put it inside a XML tags, with the tags being defined by an already-existing schema.

So it might go like this: 1- Create Dataset and fill its rows with data. 2- Create an XML according to an XML schema rules. 3- Fill said XML file with data from Dataset such that data is taken from the Dataset while structure of the XML file is taken from the XML schema.

+1  A: 

Well. The easiest way to do this would be to use a strongly typed DataSet, if you only have one schema. You can also dynamically bind an XML schema to the schema of a DataSet. I think the only trick of it is that you need to have loaded the schema before the data. If that's a problem, then create a secondary data set with the loaded schema and copy the data over.

var dataSet = new DataSet();
dataSet.ReadXmlSchema(new StringReader(@"schema goes here or something"));

Did I miss the point of your question entirely?

@Max: this will work, depending on the schema. DataSet doesn't support all schema constructs, or all schemas.
John Saunders
Thanks, the answer is correct. (I have already solved my problem shortly after asking)
Voulnet
@John: That is not a surprise. On the other hand, xsd doesn't support all schema constructs... ;)
@MaxGuernseylll,if there are multiple tables in xml file and I want tot read only one table in dataset by filetered condition then what should I do ?
Harikrishna
@Harikrishna: I'm not 100% certain. I think there is a transforming XML reader that takes an XSLT and an underlying reader. I'd probably create an XSLT that selects just the data you want, and bind it into an XSLT transforming reader so that the DataSet never even sees the XML for the tables you do not want.
@MaxGuernseylll,Will you please explain it ?
Harikrishna
+1  A: 

You can use dataset writexml method or writexmlschema method. more can be found in MSDN http://msdn.microsoft.com/en-us/library/360dye2a.aspx

Wonde
You are correct, I have used WriteXML.
Voulnet