Click here to Skip to main content
12,291,271 members (59,434 online)
Click here to Skip to main content
Add your own
alternative version

Stats

107.3K views
34 bookmarked
Posted

Convert XML Data to DataSet and Back

, 18 May 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

// 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)

Share

About the Author

S Sansanwal
Architect
Australia Australia
No Biography provided

You may also be interested in...

Comments and Discussions

 
Answerthis is Single line code Pin
venki Livestrong15-Feb-15 21:47
membervenki Livestrong15-Feb-15 21:47 
Questionthere is a Simple two line peice of code to do this Pin
venki Livestrong15-Feb-15 22:13
membervenki Livestrong15-Feb-15 22:13 
GeneralGeneral Pin
hjhjhjhjhjhjhjhjh9-Oct-12 20:50
memberhjhjhjhjhjhjhjhjh9-Oct-12 20:50 
QuestionNon Standard Xml Pin
hussain2luv28-Apr-08 5:32
memberhussain2luv28-Apr-08 5:32 
Generalthankyou Pin
Xp3ll3d13-Nov-05 19:36
memberXp3ll3d13-Nov-05 19:36 
GeneralMuch Simpler Pin
Wasia10-Nov-05 9:55
memberWasia10-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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160518.1 | Last Updated 18 May 2005
Article Copyright 2005 by S Sansanwal
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid