Click here to Skip to main content
Click here to Skip to main content

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

Comments and Discussions

 
GeneralGeneral Pinmemberhjhjhjhjhjhjhjhjh9-Oct-12 21:50 
QuestionNon Standard Xml Pinmemberhussain2luv28-Apr-08 6:32 
Generalthankyou PinmemberXp3ll3d13-Nov-05 20:36 
GeneralMuch Simpler PinmemberWasia10-Nov-05 10:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web04 | 2.8.141223.1 | Last Updated 18 May 2005
Article Copyright 2005 by S Sansanwal
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid