Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Converting dataset to xml
Message
From
27/10/2005 16:13:54
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
27/10/2005 14:59:18
General information
Forum:
ASP.NET
Category:
XML
Miscellaneous
Thread ID:
01062521
Message ID:
01062770
Views:
15
>>>Hi Cetin,
>>>
>>>thanks for your answer.
>>>
>>>
>>>>   private void WriteDataSetToXMLFile(DataSet ds, string filename)
>>>>   {
>>>>	if (ds == null) { return; }
>>>>	XmlTextWriter xmlWr = new XmlTextWriter(filename, Encoding.UTF8);
>>>>	xmlWr.Formatting = Formatting.Indented;
>>>>	XmlSerializer xs = new XmlSerializer(typeof(DataSet));
>>>>	xs.Serialize(xmlWr, ds);
>>>>	xmlWr.Close();
>>>>   }
>>>
>>>
>>>This method writes to a file.
>>>How can I change it to return the XML string as a return value of the method?
>>
>>You could use a memorystream:
>>
>>MemoryStream ms = new MemoryStream();
>>XmlTextWriter xmlWr = new XmlTextWriter(ms, Encoding.UTF8);
>>
>>Cetin
>
>Thanks Cetin, but I think I don't understand what exactly to do.
>Can you complete the function?
>I find this a bit confusing.
using System;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace myNS
{
 class myTest
 {
   public static void Main()
   {
	myTest mt = new myTest();
	DataSet myDS = mt.GetSampleDataSet();
	//mt.WriteDataSetToXMLFile(myDS,@"c:\temp\myDataSet.xml");
        string myDS_xml = mt.WriteDataSetToXMLStr(myDS);
   }

   private DataSet GetSampleDataSet()
   {
	string strCon = "Server=(local);Integrated Security=SSPI;";
	SqlConnection cn = new SqlConnection(strCon);
	SqlDataAdapter da1 = new SqlDataAdapter("select * from Northwind.dbo.Customers",cn);
	SqlDataAdapter da2 = new SqlDataAdapter("select * from Northwind.dbo.Orders",cn);
	SqlDataAdapter da3 = 
          new SqlDataAdapter("select * from Northwind.dbo.[Order Details]",cn);
	DataSet ds = new DataSet("Northwind");
	cn.Open();
	da1.FillSchema(ds, SchemaType.Source, "Customers");
	da2.FillSchema(ds, SchemaType.Source, "Orders");
	da3.FillSchema(ds, SchemaType.Source, "Order Details");
	da1.Fill(ds,"Customers");
	da2.Fill(ds,"Orders");
	da3.Fill(ds,"Order Details");
	cn.Close();
	DataRelation co = new DataRelation("CustomerOrders", 
		ds.Tables["customers"].Columns["customerID"],
		ds.Tables["orders"].Columns["customerID"],false);
	DataRelation oo = new DataRelation("OrdersOrderDetails", 
		ds.Tables["orders"].Columns["orderID"],
		ds.Tables["Order Details"].Columns["orderID"],false);

	ds.Relations.Add(co);
	ds.Relations.Add(oo);
	return ds;
   }

   private void WriteDataSetToXMLFile(DataSet ds, string filename) 
   {
	if (ds == null) { return; }
	XmlTextWriter xmlWr = new XmlTextWriter(filename, Encoding.UTF8);
	xmlWr.Formatting = Formatting.Indented;
	XmlSerializer xs = new XmlSerializer(typeof(DataSet));
	xs.Serialize(xmlWr, ds);
	xmlWr.Close();
   }

   private string WriteDataSetToXMLStr(DataSet ds) 
   {
	if (ds == null) { return String.Empty; }
        MemoryStream ms = new MemoryStream();
        XmlTextWriter xmlWr = new XmlTextWriter(ms, Encoding.UTF8);
	xmlWr.Formatting = Formatting.Indented;
	XmlSerializer xs = new XmlSerializer(typeof(DataSet));
	xs.Serialize(xmlWr, ds);
        ms.Seek(0,SeekOrigin.Begin);
        StreamReader sr = new StreamReader(ms);
        string xml = sr.ReadToEnd();
        sr.Close();
	xmlWr.Close();
        return xml;
   }
 }
}
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform