Object Array to DataSet

I’m working with a Web Service right now that returns an array of objects. What I want to do, is bind the Object Array to a DataGrid. I was able to convert the Object Array to a DataSet and then bind it to the DataGrid. 

Code Snippet

using System;
using System.Data;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
 
 
namespace BusinessObject.Util
{
    /// <summary>
    /// Utility Class for Object Arrays.
    /// </summary>
    public class ObjectArray
    {
        private Object[] _objectArray;
 
        public ObjectArray(Object[] objectArray)
        {
            this._objectArray = objectArray;
        }
 
        public DataSet ToDataSet()
        {
            DataSet ds = new DataSet();
 
            XmlSerializer xmlSerializer = 
		new XmlSerializer(_objectArray.GetType());
            StringWriter writer = new StringWriter();
            xmlSerializer.Serialize(writer, _objectArray);
            StringReader reader = 
		new StringReader(writer.ToString());
 
            ds.ReadXml(reader);
            return ds;
        }
    }
}

How to Use the ObjectArray Class with a Web Service

I would think that it’s obvious how this class should be used, but there’s always a chance that the person reading this article is new to .NET and could use an explanation.

Save the class to a folder in your project and change the Namespace of the class to match the location in your project.

Let’s say that the Web Service provided you with an array of “Persons” ( Person [ ] ). Create a new Person Array:

Person[] personList;

Call the Web Service method that returns the Person Array:

personList = Course.GetPersonList();

Instantiate the downloaded “Object Array” class with the personList:

ObjectArray objectArray = new ObjectArray(personList);

Create a DataSet and convert your ObjectArray to a DataSet using the method:

DataSet ds = new DataSet(); 
ds = objectArray.ToDataSet();

Reference Taken From
http://tom.gilki.org/programming/net/Utils/ObjectArrayDataSet/index.shtml

Advertisements

8 Responses to Object Array to DataSet

  1. sk says:

    Can this be used for the 2.0 framework?

  2. vivek says:

    Hi,
    Thanks for that article. its very nice one

  3. GaJedi says:

    Great little piece! simplified my task. Thanks!

  4. Michelle says:

    You’re awesome. Article was very helpful.

  5. dharmaraj says:

    It work nice

  6. Sean says:

    This is great, you just saved me a few hours! Thanks!

  7. Krishna says:

    Thanks for the article. You have saved a lot of my time.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: