Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Converting dataset to xml
Message
From
27/10/2005 07:04:49
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
27/10/2005 03:03:40
General information
Forum:
ASP.NET
Category:
XML
Miscellaneous
Thread ID:
01062521
Message ID:
01062556
Views:
11
>Hi,
>
>I'm trying to convert a dataset to XML string using C#.
>The string should be passed as a return value of a method.
>
>I've got 3 big problems with that.
>
>1. the dataset just gives me the XML and schema with seperate methods, but I want schema and XML in one string
>2. the XML is UTF16 encoded, but I want UTF8
>3. the schema doesn't include field length informations, with the result, that a VFP application which consumes the XML, creates Memofields for each character field
>
>Any ideas to solve these problems?

Something like this works for me well:
using System;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.Xml;
using System.Xml.Serialization;


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

   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();
   }
 }
}
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