command.Parameters.AddWithValue("@WordParam", word);
SqlDataReader reader = command.ExecuteReader();
ArrayList lista = new ArrayList();
while (reader.Read())
{
WordObj obj = new WordObj();
obj.WordObjectConst(Int32.Parse(reader.GetValue(0).ToString()), reader.GetValue(1).ToString());
lista.Add(obj);
}
connection.Close();
return lista;
}
Now when I make a call on the web method the following errors appears:
System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type AddAndReturnWords.WordObj was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically. at System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(String name, String ns, Object o, Boolean xsiType) at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType) at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write3_ArrayOfAnyType(Object o) at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayListSerializer1.Serialize(Object objectToSerialize, XmlSerializationWriter writer) at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id) --- End of inner exception stack trace ---
I know that i have to do manual serialization but i don't know how?
Can anyone help, jsust past some code or ..... :)
thanks
NOTE: I have already defined WordObj class and methods
using System.Collections.Generic; and on inicialization of the list I used List<WordObj> lista = new List<WordObj>(); instead of
List<WordObj> lista; as you specified.
It was very easy for me to make the service pass dataset but I chouse list because i expected to vuew the details in the XML format. If i pass only strings example i have nice XML format. But because the servise dont know how to serializi my custom opbject
there is no detail vuew
The XML Serializer only serializes public fields or public read/write properties of classes that have a parameterless constructor. Try this definition:
public class WordObj
{
private int _id;
private string _word;
public WordObj()
{
}
public WordObj(int id, string word)
{
_id = id;
_word = word;
}
public int Id
{
get { return _id; }
set { _id = value; }
}
public string Word
{
set { _word = value; }
get { return _word; }
}
}
aco_mk
Member
53 Points
47 Posts
How to use Use the XmlInclude or SoapInclude attribute
Jul 15, 2009 09:27 AM|LINK
Hi Gues I have the following web method
[WebMethod]
public ArrayList GetEngWordsForMakWord(string word)
{
DataSet dataSet = new DataSet();
String sql = SOME SQL THAT RETURNS DATA;
SqlConnection connection = ConnectionMenager.GetConnection();
SqlCommand command = new SqlCommand(sql, connection);
command.CommandType = CommandType.Text;
command.Parameters.AddWithValue("@WordParam", word);
SqlDataReader reader = command.ExecuteReader();
ArrayList lista = new ArrayList();
while (reader.Read())
{
WordObj obj = new WordObj();
obj.WordObjectConst(Int32.Parse(reader.GetValue(0).ToString()), reader.GetValue(1).ToString());
lista.Add(obj);
}
connection.Close();
return lista;
}
Now when I make a call on the web method the following errors appears:
I know that i have to do manual serialization but i don't know how?
Can anyone help, jsust past some code or ..... :)
thanks
NOTE: I have already defined WordObj class and methods
qwe123kids
All-Star
48619 Points
7957 Posts
MVP
Re: How to use Use the XmlInclude or SoapInclude attribute
Jul 15, 2009 11:12 AM|LINK
Hi,
http://www.codeproject.com/KB/XML/xmlserializerforunknown.aspx
http://stackoverflow.com/questions/809683/xmlinclude-or-soapinclude
Chk the above link
Avinash Tiwari
Remember to click “Mark as Answer” on the post, if it helps you.
johnwsaunder...
Star
11262 Points
1981 Posts
Re: How to use Use the XmlInclude or SoapInclude attribute
Jul 15, 2009 12:25 PM|LINK
Your code has some problems. Try this instead:
List<WordObj> lista; using (SqlConnection connection = ConnectionMenager.GetConnection()) { connection.Open(); using (SqlCommand command = new SqlCommand(sql, connection)) { command.CommandType = CommandType.Text; command.Parameters.AddWithValue("@WordParam", word); using (SqlDataReader reader = command.ExecuteReader()) { lista = new List<WordObj>(); while (reader.Read()) { WordObj obj = new WordObj(); obj.WordObjectConst( Int32.Parse(reader.GetValue(0).ToString()), reader.GetValue(1).ToString()); lista.Add(obj); } } } connection.Close(); } return lista;aco_mk
Member
53 Points
47 Posts
Re: How to use Use the XmlInclude or SoapInclude attribute
Jul 16, 2009 06:30 AM|LINK
OK, this work only i added
using System.Collections.Generic; and on inicialization of the list I used List<WordObj> lista = new List<WordObj>(); instead of List<WordObj> lista; as you specified.
I have only one question:
Now the service XML looks like following:
<ArrayOfWordObj>
<WordObj/>
<WordObj/>
</ArrayOfWordObj>
Can i make it to look somthing as follow: to vuew the details?
<ArrayOfWordObj>
<WordObj>
<word> theWord </word>
<id>idvalue</id>
</WordObj>
</ArrayOfWordObj>
It was very easy for me to make the service pass dataset but I chouse list because i expected to vuew the details in the XML format. If i pass only strings example i have nice XML format. But because the servise dont know how to serializi my custom opbject there is no detail vuew
johnwsaunder...
Star
11262 Points
1981 Posts
Re: How to use Use the XmlInclude or SoapInclude attribute
Jul 16, 2009 09:28 AM|LINK
You'll have to post the code for WordObj.
aco_mk
Member
53 Points
47 Posts
Re: How to use Use the XmlInclude or SoapInclude attribute
Jul 16, 2009 11:12 AM|LINK
Thank you very much for you're help :)
The variables whare define as private thats why they waren't parse as it should for the XML
i've dine them as public and wverything is as should.
Thanks again
BR
johnwsaunder...
Star
11262 Points
1981 Posts
Re: How to use Use the XmlInclude or SoapInclude attribute
Jul 16, 2009 11:22 AM|LINK
The XML Serializer only serializes public fields or public read/write properties of classes that have a parameterless constructor. Try this definition:
public class WordObj { private int _id; private string _word; public WordObj() { } public WordObj(int id, string word) { _id = id; _word = word; } public int Id { get { return _id; } set { _id = value; } } public string Word { set { _word = value; } get { return _word; } } }aco_mk
Member
53 Points
47 Posts
Re: How to use Use the XmlInclude or SoapInclude attribute
Jul 16, 2009 11:31 AM|LINK
Thanks again I allready post that i've fond that the field must bi pblick :)))
but i will try you're code above.
BR