Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataSet.WriteXml to string

Tags:

c#

.net

dataset

I'm tring to get a string from a DataSet without using GetXml. I'm using WriteXml, instead. How to use it to get a string? Thanks

like image 469
pistacchio Avatar asked Jun 08 '09 08:06

pistacchio


People also ask

What is the use of WriteXml() Method in asp net?

The WriteXml method provides a way to write either data only, or both data and schema from a DataSet into an XML document, whereas the WriteXmlSchema method writes only the schema. To write both data and schema, use one of the overloads that includes the mode parameter, and set its value to WriteSchema .

Is a DataSet XML?

In an XML representation of a DataSet, the data is written in XML and the schema, if it is included inline in the representation, is written using the XML Schema definition language (XSD). XML and XML Schema provide a convenient format for transferring the contents of a DataSet to and from remote clients.

How do you read from and write to DataSet using XML?

The XmlTextReader, XmlNodeReader and XmlValidatingReader classes are derived from XmlReader class. As their name explains, they are used to read text, node, and schemas. The XmlWrite class contains functionality to write data to XML documents. This class provides many write method to write XML document items.

How do I create an XML DataSet?

All you need to do is call the WriteXml method of your DataSet and pass the full path of the XML file. This example creates a DataSet, fills the data for the DataSet, and writes the data to an XML file.


2 Answers

StringWriter sw = new StringWriter(); dataSet.WriteXml(sw); string result = sw.ToString(); 
like image 52
mmx Avatar answered Oct 09 '22 04:10

mmx


Write to a StringWriter, and then call ToString on that.

Note that if you want the generated XML declaration to specify UTF-8 instead of UTF-16, you'll need something like my Utf8StringWriter.

like image 30
Jon Skeet Avatar answered Oct 09 '22 02:10

Jon Skeet