vema reddy

vema reddy

  • NA
  • 38
  • 4.8k

How can i convert form Xml string (Not From Xml File)into list of objects

Jul 26 2012 4:28 AM
Hi,


                  I am doing silverlight project by using WCF. am unable to deSerilize the xml string into list of objects. I had search for solution but i did't find sol.


The error is :


Error in line 1 position 13. Expecting element 'ArrayOfContactService.newDataSet' from namespace 'http://schemas.datacontract.org/2004/07/AddingContacts.Web'.. Encountered 'Element'  with name 'NewDataSet', namespace ''.


Here NewDataSet is while creating dataset it was created automatically


 can u help me ASAP.
         Please find the bellow code






my xml string is :


 
<NewDataSet>
<Table>
<Name>vema</Name>
<Age>24</Age>
<Email>[email protected]</Email>
<Mobile>589787759</Mobile>
<Address>gfjsag</Address>
<CreatedDate>2012-07-24T17:36:29.25+05:30</CreatedDate>
</Table>
<Table>
<Name>Rama Krishna</Name>
<Age>25</Age>
<Email>[email protected]</Email>
<Mobile>6558689j</Mobile>
<Address>hjhjhjh</Address>
<CreatedDate>2012-07-24T18:46:08.877+05:30</CreatedDate>
</Table>
</NewDataSet>     

         This is my Service Class





using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Web.Configuration;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;


namespace AddingContacts.Web
{
    [ServiceContract(Namespace = "")]
    [SilverlightFaultBehavior]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ContactService
    {
        [OperationContract]
        public void DoWork()
        {
            // Add your operation implementation here
            return;
        }
        /// &lt;summary&gt;
        /// This class is used as datatype for generic list for send the data over the network 
        /// 
        [DataContract]
        public class newDataSet
        {
              public List&lt;table&gt; NewDataSet=new List&lt;table&gt;();
        }
        [DataContract]
        public class table
        {
            [DataMember]
            public List&lt;contacts&gt; Table = new List&lt;contacts&gt;();
        }
       
        [DataContract]
        public class contacts
        {
            [DataMember]
            public string Name { get; set; }
            [DataMember]
            public string Age { get; set; }
            [DataMember]
            public string Email { get; set; }
            [DataMember]
            public string Mobile { get; set; }
            [DataMember]
            public string Address { get; set; }
            [DataMember]
            public DateTime CreatedDate { get; set; }
        }
        /// &lt;summary&gt;
        /// This Method is used to show all contacts in the gridview of silverlight popups
        /// &lt;/summary&gt;
        /// Created By: Vema Reddy
        /// &lt;returns&gt;&lt;/returns&gt;
        [OperationContract]
        public List&lt;newDataSet&gt; fetchContacts()
        {


            SqlConnection con = new SqlConnection();
            con.ConnectionString = WebConfigurationManager.ConnectionStrings["addContactConnectoionString"].ConnectionString;
            SqlCommand cmd = new SqlCommand("USP_GetContacts", con);
            cmd.CommandType = CommandType.StoredProcedure;
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            con.Close();
            da.Fill(ds);
            string resultxml = ds.GetXml();
            List&lt;newDataSet&gt; listobj = new List&lt;newDataSet&gt;();
            try
            {
                using (StringReader reader = new StringReader(resultxml))
                {
                    using (XmlReader xmlReader = XmlReader.Create(reader))
                    {
                        var serializer = new DataContractSerializer(typeof(List&lt;newDataSet&gt;));
                        MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(resultxml));
                        listobj = (List&lt;newDataSet&gt;)serializer.ReadObject(stream);
                    }
            }
                   
           
            catch (Exception ex)
            {


            }
            finally
            {
               
            }
            return listobj;






       


   }
}    

Answers (6)