469,946 Members | 1,390 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,946 developers. It's quick & easy.

web service error - urgent

I'm creating a web service this is going to return an arraylist of items.
When I try and run it Im' getting the following error message:
System.InvalidOperationException: There was an error generating the XML
document. ---System.InvalidOperationException: The type Car List was not
expected. Use the XmlInclude or SoapInclude attribute to specify types that
are not known statically.
at
System.Xml.Serialization.XmlSerializationWriter.Wr iteTypedPrimitive(String
name, String ns, Object o, Boolean xsiType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationWriter1.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationWriter1.Write3_ArrayOfAnyType(Object o)
at
Microsoft.Xml.Serialization.GeneratedAssembly.Arra yListSerializer.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter xmlWriter,
Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter xmlWriter,
Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
at System.Xml.Serialization.XmlSerializer.Serialize(T extWriter
textWriter, Object o)
at System.Web.Services.Protocols.XmlReturnWriter.Writ e(HttpResponse
response, Stream outputStream, Object returnValue)
at System.Web.Services.Protocols.HttpServerProtocol.W riteReturns(Object[]
returnValues, Stream outputStream)
at System.Web.Services.Protocols.WebServiceHandler.Wr iteReturns(Object[]
returnValues)
at System.Web.Services.Protocols.WebServiceHandler.In voke()
here is my code Im' trying to use.
[WebMethod]
[return: System.Xml.Serialization.XmlArrayItem(typeof(Cars) )]
public ArrayList CarList
{
string sql = "SELECT * from Cars";
SqlConnection conn = new
SqlConnection(ConfigurationManager.AppSettings["connectMe"]);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
SqlDataReader reader;
reader = cmd.ExecuteReader();

ArrayList carList= new ArrayList();
while (reader.Read())
{
Cars car = new Cars
car.make= eader.GetString(1);
car.model= reader.GetString(2);
carList.Add(car)
}
reader.Close();
conn.Close();

return ports;
}

public class Cars
{
public string make;
public string cars;
}

what am i missing or doing wrong?
Nov 10 '06 #1
5 4415
What is "ports" anyway? I don't see this defined anywhere in your method.
(return ports;)?

What about this instead (return an array of Cars):
return carList.ToArray(typeof(Car));
--
rgds.
/Claus Konrad
MCSD.NET (C#)
"igotyourdotnet" wrote:
I'm creating a web service this is going to return an arraylist of items.
When I try and run it Im' getting the following error message:
System.InvalidOperationException: There was an error generating the XML
document. ---System.InvalidOperationException: The type Car List was not
expected. Use the XmlInclude or SoapInclude attribute to specify types that
are not known statically.
at
System.Xml.Serialization.XmlSerializationWriter.Wr iteTypedPrimitive(String
name, String ns, Object o, Boolean xsiType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationWriter1.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationWriter1.Write3_ArrayOfAnyType(Object o)
at
Microsoft.Xml.Serialization.GeneratedAssembly.Arra yListSerializer.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter xmlWriter,
Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter xmlWriter,
Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
at System.Xml.Serialization.XmlSerializer.Serialize(T extWriter
textWriter, Object o)
at System.Web.Services.Protocols.XmlReturnWriter.Writ e(HttpResponse
response, Stream outputStream, Object returnValue)
at System.Web.Services.Protocols.HttpServerProtocol.W riteReturns(Object[]
returnValues, Stream outputStream)
at System.Web.Services.Protocols.WebServiceHandler.Wr iteReturns(Object[]
returnValues)
at System.Web.Services.Protocols.WebServiceHandler.In voke()
here is my code Im' trying to use.
[WebMethod]
[return: System.Xml.Serialization.XmlArrayItem(typeof(Cars) )]
public ArrayList CarList
{
string sql = "SELECT * from Cars";
SqlConnection conn = new
SqlConnection(ConfigurationManager.AppSettings["connectMe"]);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
SqlDataReader reader;
reader = cmd.ExecuteReader();

ArrayList carList= new ArrayList();
while (reader.Read())
{
Cars car = new Cars
car.make= eader.GetString(1);
car.model= reader.GetString(2);
carList.Add(car)
}
reader.Close();
conn.Close();

return ports;
}

public class Cars
{
public string make;
public string cars;
}

what am i missing or doing wrong?
Nov 10 '06 #2
ports is a type o, it should be CarList

"igotyourdotnet" wrote:
I'm creating a web service this is going to return an arraylist of items.
When I try and run it Im' getting the following error message:
System.InvalidOperationException: There was an error generating the XML
document. ---System.InvalidOperationException: The type Car List was not
expected. Use the XmlInclude or SoapInclude attribute to specify types that
are not known statically.
at
System.Xml.Serialization.XmlSerializationWriter.Wr iteTypedPrimitive(String
name, String ns, Object o, Boolean xsiType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationWriter1.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationWriter1.Write3_ArrayOfAnyType(Object o)
at
Microsoft.Xml.Serialization.GeneratedAssembly.Arra yListSerializer.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter xmlWriter,
Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter xmlWriter,
Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
at System.Xml.Serialization.XmlSerializer.Serialize(T extWriter
textWriter, Object o)
at System.Web.Services.Protocols.XmlReturnWriter.Writ e(HttpResponse
response, Stream outputStream, Object returnValue)
at System.Web.Services.Protocols.HttpServerProtocol.W riteReturns(Object[]
returnValues, Stream outputStream)
at System.Web.Services.Protocols.WebServiceHandler.Wr iteReturns(Object[]
returnValues)
at System.Web.Services.Protocols.WebServiceHandler.In voke()
here is my code Im' trying to use.
[WebMethod]
[return: System.Xml.Serialization.XmlArrayItem(typeof(Cars) )]
public ArrayList CarList
{
string sql = "SELECT * from Cars";
SqlConnection conn = new
SqlConnection(ConfigurationManager.AppSettings["connectMe"]);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
SqlDataReader reader;
reader = cmd.ExecuteReader();

ArrayList carList= new ArrayList();
while (reader.Read())
{
Cars car = new Cars
car.make= eader.GetString(1);
car.model= reader.GetString(2);
carList.Add(car)
}
reader.Close();
conn.Close();

return ports;
}

public class Cars
{
public string make;
public string cars;
}

what am i missing or doing wrong?
Nov 10 '06 #3
this errors out.
return carList.ToArray(typeof(Car));

'Can't convert ArrayList to Array'

"Claus Konrad [MCSD]" wrote:
What is "ports" anyway? I don't see this defined anywhere in your method.
(return ports;)?

What about this instead (return an array of Cars):
return carList.ToArray(typeof(Car));
--
rgds.
/Claus Konrad
MCSD.NET (C#)
"igotyourdotnet" wrote:
I'm creating a web service this is going to return an arraylist of items.
When I try and run it Im' getting the following error message:
System.InvalidOperationException: There was an error generating the XML
document. ---System.InvalidOperationException: The type Car List was not
expected. Use the XmlInclude or SoapInclude attribute to specify types that
are not known statically.
at
System.Xml.Serialization.XmlSerializationWriter.Wr iteTypedPrimitive(String
name, String ns, Object o, Boolean xsiType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationWriter1.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationWriter1.Write3_ArrayOfAnyType(Object o)
at
Microsoft.Xml.Serialization.GeneratedAssembly.Arra yListSerializer.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter xmlWriter,
Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter xmlWriter,
Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
at System.Xml.Serialization.XmlSerializer.Serialize(T extWriter
textWriter, Object o)
at System.Web.Services.Protocols.XmlReturnWriter.Writ e(HttpResponse
response, Stream outputStream, Object returnValue)
at System.Web.Services.Protocols.HttpServerProtocol.W riteReturns(Object[]
returnValues, Stream outputStream)
at System.Web.Services.Protocols.WebServiceHandler.Wr iteReturns(Object[]
returnValues)
at System.Web.Services.Protocols.WebServiceHandler.In voke()
here is my code Im' trying to use.
[WebMethod]
[return: System.Xml.Serialization.XmlArrayItem(typeof(Cars) )]
public ArrayList CarList
{
string sql = "SELECT * from Cars";
SqlConnection conn = new
SqlConnection(ConfigurationManager.AppSettings["connectMe"]);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
SqlDataReader reader;
reader = cmd.ExecuteReader();

ArrayList carList= new ArrayList();
while (reader.Read())
{
Cars car = new Cars
car.make= eader.GetString(1);
car.model= reader.GetString(2);
carList.Add(car)
}
reader.Close();
conn.Close();

return ports;
}

public class Cars
{
public string make;
public string cars;
}

what am i missing or doing wrong?
Nov 10 '06 #4
A good design practice in SOA is to make typesafe contracts.
This means, that instead of returning ArrayList, do declare Car and return
Car[] instead.

like this:

[WebMethod()]
public Car[] GetCars(some parameter...)
{
..stuff
}

[Serializable]
public class Car
{
...properties
}

See this link for example:
http://msdn.microsoft.com/newsgroups...aa2&sloc=en-us
--
rgds.
/Claus Konrad
MCSD.NET (C#)
"igotyourdotnet" wrote:
I'm creating a web service this is going to return an arraylist of items.
When I try and run it Im' getting the following error message:
System.InvalidOperationException: There was an error generating the XML
document. ---System.InvalidOperationException: The type Car List was not
expected. Use the XmlInclude or SoapInclude attribute to specify types that
are not known statically.
at
System.Xml.Serialization.XmlSerializationWriter.Wr iteTypedPrimitive(String
name, String ns, Object o, Boolean xsiType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationWriter1.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationWriter1.Write3_ArrayOfAnyType(Object o)
at
Microsoft.Xml.Serialization.GeneratedAssembly.Arra yListSerializer.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter xmlWriter,
Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter xmlWriter,
Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
at System.Xml.Serialization.XmlSerializer.Serialize(T extWriter
textWriter, Object o)
at System.Web.Services.Protocols.XmlReturnWriter.Writ e(HttpResponse
response, Stream outputStream, Object returnValue)
at System.Web.Services.Protocols.HttpServerProtocol.W riteReturns(Object[]
returnValues, Stream outputStream)
at System.Web.Services.Protocols.WebServiceHandler.Wr iteReturns(Object[]
returnValues)
at System.Web.Services.Protocols.WebServiceHandler.In voke()
here is my code Im' trying to use.
[WebMethod]
[return: System.Xml.Serialization.XmlArrayItem(typeof(Cars) )]
public ArrayList CarList
{
string sql = "SELECT * from Cars";
SqlConnection conn = new
SqlConnection(ConfigurationManager.AppSettings["connectMe"]);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
SqlDataReader reader;
reader = cmd.ExecuteReader();

ArrayList carList= new ArrayList();
while (reader.Read())
{
Cars car = new Cars
car.make= eader.GetString(1);
car.model= reader.GetString(2);
carList.Add(car)
}
reader.Close();
conn.Close();

return ports;
}

public class Cars
{
public string make;
public string cars;
}

what am i missing or doing wrong?
Nov 10 '06 #5
Hi Mike,

Your original code is correct, you should return the ArrayList instance
rather than the ArrayList.ToArray since your method's signature has marked
the return type as "ArrayList".

For your original problem
===========
System.InvalidOperationException: The type Car List was not
expected. Use the XmlInclude or SoapInclude attribute to specify types that
are not known statically.
===========

As the error message indicate, you should add the "XmlInclude" attribute to
specify the "Cars" type (or any other derived class type from Object you
will add into ArrayList). For example:

====================
[WebMethod]
[XmlInclude(typeof(Cars))]
[return: XmlArrayItem(typeof(Cars))]
public ArrayList CarList()
{
ArrayList list = new ArrayList();

for (int i = 0; i < 5; i++)
{
Cars car = new Cars();
car.cars = "cars_" + i;
car.make = "make_" + i;

list.Add(car);
}

return list;
}
======================

You can also add the XmlInclude attribute at class level such as below so
that all the webmethods in the class can get this applied.

=========
[XmlInclude(typeof(Cars))]
public class ArrayListService : System.Web.Services.WebService
{
=========

This attribute is to make the XMLSerialization for a class or a method be
aware of some certain derived object types.

Hope this helps some.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 13 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jason Richmeier | last post: by
7 posts views Thread by =?Utf-8?B?Sm9obi5ORVQ=?= | last post: by
6 posts views Thread by =?Utf-8?B?RGFuaWVs?= | last post: by
6 posts views Thread by Michael Tissington | last post: by
5 posts views Thread by =?Utf-8?B?QmlsbHkgWmhhbmc=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.