Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#
Article

Convert XML Data to DataSet and Back

Rate me:
Please Sign up or sign in to vote.
2.57/5 (16 votes)
18 May 2005CPOL 142.4K   34   8
This article describes the functions to convert the XML data to an untyped dataset and get the XML back from untyped dataset

Using the Code

Use this function to save an XML string to DataSet:

C#
// Function to convert passed XML data to dataset
 public DataSet ConvertXMLToDataSet(string xmlData)
 {
  StringReader stream = null;
  XmlTextReader reader = null;
  try
  {
   DataSet xmlDS = new DataSet() ;
   stream = new StringReader(xmlData);
   // Load the XmlTextReader from the stream
   reader = new XmlTextReader(stream);
   xmlDS.ReadXml(reader);
   return xmlDS;
  }
  catch
  {
   return null;
  }
  finally
  {
   if(reader != null) reader.Close();
  }
 }// Use this function to get XML string from a dataset

 // Function to convert passed dataset to XML data
 public string ConvertDataSetToXML(DataSet xmlDS)
 {
  MemoryStream stream = null;
  XmlTextWriter writer = null;
  try
  {
   stream = new MemoryStream();
   // Load the XmlTextReader from the stream
   writer = new XmlTextWriter(stream, Encoding.Unicode);
   // Write to the file with the WriteXml method.
   xmlDS.WriteXml(writer);
   int count = (int) stream.Length;
   byte[] arr = new byte[count];
   stream.Seek(0, SeekOrigin.Begin);
   stream.Read(arr, 0, count);
   UnicodeEncoding utf = new UnicodeEncoding();
   return utf.GetString(arr).Trim();
  }
  catch
  {
   return String.Empty ;
  }
  finally
  {
   if(writer != null) writer.Close();
  }
 }

History

  • 18th May, 2005: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionCannot add constraint to DataTable 'PricingDetailList' which is a child table in two nested relations. Pin
Asutosha12-Oct-17 5:52
professionalAsutosha12-Oct-17 5:52 
Answerthis is Single line code Pin
venki Livestrong15-Feb-15 21:47
venki Livestrong15-Feb-15 21:47 
Questionthere is a Simple two line peice of code to do this Pin
venki Livestrong15-Feb-15 22:13
venki Livestrong15-Feb-15 22:13 
GeneralGeneral Pin
hjhjhjhjhjhjhjhjh9-Oct-12 20:50
hjhjhjhjhjhjhjhjh9-Oct-12 20:50 
QuestionNon Standard Xml Pin
hussain2luv28-Apr-08 5:32
hussain2luv28-Apr-08 5:32 
Generalthankyou Pin
Xp3ll3d13-Nov-05 19:36
Xp3ll3d13-Nov-05 19:36 
GeneralMuch Simpler Pin
Wasia10-Nov-05 9:55
Wasia10-Nov-05 9:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.