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;
}
/// <summary>
/// This class is used as datatype for generic list for send the data over the network
///
[DataContract]
public class newDataSet
{
public List<table> NewDataSet=new List<table>();
}
[DataContract]
public class table
{
[DataMember]
public List<contacts> Table = new List<contacts>();
}
[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; }
}
/// <summary>
/// This Method is used to show all contacts in the gridview of silverlight popups
/// </summary>
/// Created By: Vema Reddy
/// <returns></returns>
[OperationContract]
public List<newDataSet> 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<newDataSet> listobj = new List<newDataSet>();
try
{
using (StringReader reader = new StringReader(resultxml))
{
using (XmlReader xmlReader = XmlReader.Create(reader))
{
var serializer = new DataContractSerializer(typeof(List<newDataSet>));
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(resultxml));
listobj = (List<newDataSet>)serializer.ReadObject(stream);
}
}
catch (Exception ex)
{
}
finally
{
}
return listobj;
}
}
Loading
vema reddyPosted Jul 26, 2012, 6:27 AM
Thank you spending your valuable time for me.
Atul KumarPosted Jul 26, 2012, 6:02 AM
But you can achieve this by no of ways.
1. using for loop like you did.
2. using foreach loop.
3. consuming XML and then using LINQ to XML to convert XML to objects.
you can convert XML to C# objects using normal XML reader classed or
simplest way is LINQ to XML(if you know) as I already mentioned.
vema reddyPosted Jul 26, 2012, 5:38 AM
Thanks for your reply
I already done this
see my code
can u do it in another way that why i am doing like this .
is there any performance diff between this code and that code
one more thing is it possible that convertion xml string to objects
If you don't mind Please tell me
Atul KumarPosted Jul 26, 2012, 5:02 AM
List
foreach(var dr in ds.rows)
{
contacts.add(new Contact(){ contact.Name = dr["Name"]});
......................................
}
return contacts
vema reddyPosted Jul 26, 2012, 4:52 AM
Thanks for your reply.
But here problem is whenever i am convering Xml to List of objects
it won't take that input .
afrter geting this objects i need to attach to my datagrid which is in xaml.cs
Atul KumarPosted Jul 26, 2012, 4:35 AM