Click here to Skip to main content
Licence CPOL
First Posted 18 May 2005
Views 56,758
Bookmarked 31 times

Convert XML Data to DataSet and Back

By S Sansanwal | 18 May 2005
This article describes the functions to convert the XML data to an untyped dataset and get the XML back from untyped dataset
5 votes, 35.7%
1
1 vote, 7.1%
2
1 vote, 7.1%
3
3 votes, 21.4%
4
4 votes, 28.6%
5
2.41/5 - 14 votes
μ 2.41, σa 3.07 [?]

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)

About the Author

S Sansanwal

Architect

Australia Australia

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionNon Standard Xml Pinmemberhussain2luv6:32 28 Apr '08  
Generalthankyou PinmemberXp3ll3d20:36 13 Nov '05  
GeneralMuch Simpler PinmemberWasia10:55 10 Nov '05  

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.

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