新手请教:调用webserver 得到在xml全是的没格式

wmgcn 2014-11-17 12:11:20
得到的是
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://127.0.0.1:10001">
<string>铅笔</string>
<string>13</string>
<string>钢笔</string>
<string>3</string>
<string>A4纸</string>
<string>3</string>
</ArrayOfString>

我想要的是每一组数据都是序号,每一组里面要是分类。如第一组物料名称是铅笔,数量是13.有标记组别、有分类,以便后面提取数据。下面是我在网上找的示例。
下面是webserver 方法:
 public class DBOperation:IDisposable
{
public static SqlConnection sqlCon; //用于连接数据库

//将下面的引号之间的内容换成上面记录下的属性中的连接字符串
//dfasdlfasdfsad
private String ConServerStr = @"Data Source=127.0.0.1;Initial Catalog=Data1; User ID=sa;Password=sa";

//默认构造函数
public DBOperation()
{
if (sqlCon == null)
{
sqlCon = new SqlConnection();
sqlCon.ConnectionString = ConServerStr;
sqlCon.Open();
}
}

//关闭/销毁函数,相当于Close()
public void Dispose()
{
if (sqlCon != null)
{
sqlCon.Close();
sqlCon = null;
}
}

/// <summary>
/// 获取所有货物的信息
/// </summary>
/// <returns>所有货物信息</returns>
public List<string> selectAllCargoInfor()
{
List<string> list = new List<string>();


try
{
string sql = "select ProdDesc,Stock from StockMange where Stock>0";
SqlCommand cmd = new SqlCommand(sql,sqlCon);
SqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
//将结果集信息添加到返回向量中

list.Add(reader[0].ToString());
list.Add(reader[1].ToString());


}

reader.Close();
cmd.Dispose();

}
catch(Exception)
{

}
return list;
}

}
}

下面是调用:

public class Service1 : System.Web.Services.WebService
{
DBOperation dbOperation = new DBOperation();

[WebMethod]


[WebMethod(Description = "获取所有货物的信息")]
public string[] selectAllCargoInfor()
{
return dbOperation.selectAllCargoInfor().ToArray();
}
...全文
420 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
宝_爸 2014-11-18
  • 打赏
  • 举报
回复
public Product() 就是 无参数的构造函数
宝_爸 2014-11-18
  • 打赏
  • 举报
回复

public class Product
        {
            private string M_ProdDesc; //产品描述
            private int M_Stock; //产品数量
            //创建Product对象
            public Product()
            {
            }

            public Product(string ProdDesc, int Stock)
            {
                this.M_ProdDesc = ProdDesc;
                this.M_Stock = Stock;
            }
            //姓名
            public string ProdDesc
            {
                get { return M_ProdDesc; }
            }
            //年龄
            public int Stock
            {
                get { return M_Stock; }
            }
        }
wmgcn 2014-11-18
  • 打赏
  • 举报
回复
引用 9 楼 findcaiyzh 的回复:
说的很清楚了, StockManageWebservice.DBOperation.Product 无法序列化,因为它没有无参数的构造函数。 加一个就可以了。
不懂,找了个百度,没找到什么有用信息。你有具体代码吗,那个我能看懂。需要麻烦您了一下了。
宝_爸 2014-11-18
  • 打赏
  • 举报
回复
说的很清楚了, StockManageWebservice.DBOperation.Product 无法序列化,因为它没有无参数的构造函数。 加一个就可以了。
wmgcn 2014-11-18
  • 打赏
  • 举报
回复
引用 6 楼 findcaiyzh 的回复:
我大体修改的代码 /// <summary> /// 获取所有货物的信息 /// </summary> /// <returns>所有货物信息</returns> public List<Product> selectAllCargoInfor() { List<Product> list = new List<Product>(); try { string sql = "select ProdDesc,Stock from StockMange where Stock>0"; SqlCommand cmd = new SqlCommand(sql,sqlCon); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { //将结果集信息添加到返回向量中 list.Add(new Product(reader.GetString(0), reader.GetInt32(1))); } reader.Close(); cmd.Dispose(); } catch(Exception) { } return list; } WebService返回List<Product>或者Product[]都可以。
“/”应用程序中的服务器错误。

StockManageWebservice.DBOperation.Product 无法序列化,因为它没有无参数的构造函数。

说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.InvalidOperationException: StockManageWebservice.DBOperation.Product 无法序列化,因为它没有无参数的构造函数。

源错误: 

执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。

堆栈跟踪: 


[InvalidOperationException: StockManageWebservice.DBOperation.Product 无法序列化,因为它没有无参数的构造函数。]
   System.Xml.Serialization.TypeDesc.CheckSupported() +3949099
   System.Xml.Serialization.TypeDesc.CheckSupported() +3949220
   System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError) +170
   System.Xml.Serialization.XmlReflectionImporter.ImportMemberMapping(XmlReflectionMember xmlReflectionMember, String ns, XmlReflectionMember[] xmlReflectionMembers, Boolean rpc, Boolean openModel, RecursionLimiter limiter) +65
   System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(XmlReflectionMember[] xmlReflectionMembers, String ns, Boolean hasWrapperElement, Boolean rpc, Boolean openModel, RecursionLimiter limiter) +236

[InvalidOperationException: 反射“selectAllCargoInforResult”时出错。]
   System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(XmlReflectionMember[] xmlReflectionMembers, String ns, Boolean hasWrapperElement, Boolean rpc, Boolean openModel, RecursionLimiter limiter) +843
   System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(String elementName, String ns, XmlReflectionMember[] members, Boolean hasWrapperElement, Boolean rpc, Boolean openModel, XmlMappingAccess access) +130
   System.Web.Services.Protocols.SoapReflector.ImportMembersMapping(XmlReflectionImporter xmlImporter, SoapReflectionImporter soapImporter, Boolean serviceDefaultIsEncoded, Boolean rpc, SoapBindingUse use, SoapParameterStyle paramStyle, String elementName, String elementNamespace, Boolean nsIsDefault, XmlReflectionMember[] members, Boolean validate, Boolean openModel, String key, Boolean writeAccess) +223
   System.Web.Services.Protocols.SoapReflector.ReflectMethod(LogicalMethodInfo methodInfo, Boolean client, XmlReflectionImporter xmlImporter, SoapReflectionImporter soapImporter, String defaultNs) +2989

[InvalidOperationException: 无法反射方法 Service1.selectAllCargoInfor。]
   System.Web.Services.Protocols.SoapReflector.ReflectMethod(LogicalMethodInfo methodInfo, Boolean client, XmlReflectionImporter xmlImporter, SoapReflectionImporter soapImporter, String defaultNs) +6527
   System.Web.Services.Description.SoapProtocolReflector.ReflectMethod() +91
   System.Web.Services.Description.ProtocolReflector.ReflectBinding(ReflectedBinding reflectedBinding) +1798
   System.Web.Services.Description.ProtocolReflector.Reflect() +703
   System.Web.Services.Description.ServiceDescriptionReflector.ReflectInternal(ProtocolReflector[] reflectors) +394
   System.Web.Services.Description.ServiceDescriptionReflector.Reflect(Type type, String url) +180
   System.Web.Services.Protocols.DocumentationServerType..ctor(Type type, String uri) +156
   System.Web.Services.Protocols.DocumentationServerProtocol.Initialize() +284
   System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response) +50
   System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing) +77

[InvalidOperationException: 无法处理请求。]
   System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing) +285
   System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response) +183

[InvalidOperationException: 处理请求失败。]
   System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response) +354
   System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler(HttpContext context, String verb, String url, String filePath) +212
   System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated) +47
   System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig) +193
   System.Web.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +93
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
调试失败了
wmgcn 2014-11-18
  • 打赏
  • 举报
回复
数据查询是没问题的,我在SQL2008里都能查出数据。
wmgcn 2014-11-18
  • 打赏
  • 举报
回复
引用 12 楼 findcaiyzh 的回复:
public Product() 就是 无参数的构造函数
namespace StckQtyManageWebservice
{
    public class DBOperation : IDisposable
    {
        public static SqlConnection sqlCon;  //用于连接数据库
        //将下面的引号之间的内容换成上面记录下的属性中的连接字符串
        //dfasdlfasdfsad
        private String ConServerStr = @"Data Source=127.0.0.1;Initial Catalog=XXX; User ID=sa;Password=sa";
        
        public class Product
        {
            private string M_ProdDesc;
            private Int32 M_Stock;
            public Product()
            {
            }
            public Product(string ProdDesc, int Stock)
            {
                this.M_ProdDesc = ProdDesc;
                this.M_Stock = Stock;
            }
            
            public string ProdDesc
            {
                get { return M_ProdDesc; }
                set { this.M_ProdDesc = value; }
            }
         
            public int Stock
            {
                get { return M_Stock; }
                set { this.M_Stock = value; }
            }
        }
        public DBOperation()
        {
            if (sqlCon == null)
            {
                sqlCon = new SqlConnection();
                sqlCon.ConnectionString = ConServerStr;
                sqlCon.Open();
            }
        }
        //关闭/销毁函数,相当于Close()
        public void Dispose()
        {
            if (sqlCon != null)
            {
                sqlCon.Close();
                sqlCon = null;
            }
        }
        /// <summary>
        /// 获取所有货物的信息
        /// </summary>
        /// <returns>所有货物信息</returns>
        public List<Product> selectAllCargoInfor()
        {
            List<Product> list = new List<Product>();
            try
            {
               string sql = "select ProdDesc,Stock from StockMange where Stock>0";
                SqlCommand cmd = new SqlCommand(sql,sqlCon);
                SqlDataReader reader = cmd.ExecuteReader();
               while (reader.Read())
                {
                    //将结果集信息添加到返回向量中
                    
                    list.Add(new Product(reader.GetString(0), reader.GetInt32(1)));
                }
                reader.Close();
                cmd.Dispose();
            }
            catch (Exception)
            {
            }
            return list;
       
        }
    }
}

namespace StockManageWebservice
{
    /// <summary>  
    /// Service1 的摘要说明  
    /// </summary>  
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。  
    // [System.Web.Script.Services.ScriptService]  
    public class Service1 : System.Web.Services.WebService
    {
        DBOperation dbOperation = new DBOperation();
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod(Description = "获取所有货物的信息")]
        public StckQtyManageWebservice.DBOperation.Product[] selectAllCargoInfor()
        {
            return dbOperation.selectAllCargoInfor().ToArray();
        }
        
    }
}
没什么报错了,调用方法网页返回数据只有: This XML file does not appear to have any style information associated with it. The document tree is shown below. <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/"/>
宝_爸 2014-11-18
  • 打赏
  • 举报
回复
其实想你原来那样返回List<String>或者String[]也不是不可以,客户端也按照一个productdescirpt一个stock解析。 只是维护性有点差而已。
宝_爸 2014-11-18
  • 打赏
  • 举报
回复
我大体修改的代码 /// <summary> /// 获取所有货物的信息 /// </summary> /// <returns>所有货物信息</returns> public List<Product> selectAllCargoInfor() { List<Product> list = new List<Product>(); try { string sql = "select ProdDesc,Stock from StockMange where Stock>0"; SqlCommand cmd = new SqlCommand(sql,sqlCon); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { //将结果集信息添加到返回向量中 list.Add(new Product(reader.GetString(0), reader.GetInt32(1))); } reader.Close(); cmd.Dispose(); } catch(Exception) { } return list; } WebService返回List<Product>或者Product[]都可以。
wmgcn 2014-11-17
  • 打赏
  • 举报
回复
引用 1 楼 findcaiyzh 的回复:
你放回的就是string数组,我的觉得没有问题啊。你要有格式需要定义自己的类。 例如 class Product { public string ProdDesc {get;set;} public int Stock{get;set;} } [WebMethod(Description = "获取所有货物的信息")] public Product[] selectAllCargoInfor() { ..... }
非常感谢您的回复,我试了一下,提示”无法将string[]隐式转换为XXX.XXX .server1. Product[]",是不是还有其他地方要改。麻烦您再帮我看一下。
宝_爸 2014-11-17
  • 打赏
  • 举报
回复
你放回的就是string数组,我的觉得没有问题啊。你要有格式需要定义自己的类。 例如 class Product { public string ProdDesc {get;set;} public int Stock{get;set;} } [WebMethod(Description = "获取所有货物的信息")] public Product[] selectAllCargoInfor() { ..... }
wmgcn 2014-11-17
  • 打赏
  • 举报
回复
引用 4 楼 findcaiyzh 的回复:
[quote=引用 2 楼 wmgcn 的回复:] [quote=引用 1 楼 findcaiyzh 的回复:] 你放回的就是string数组,我的觉得没有问题啊。你要有格式需要定义自己的类。 例如 class Product { public string ProdDesc {get;set;} public int Stock{get;set;} } [WebMethod(Description = "获取所有货物的信息")] public Product[] selectAllCargoInfor() { ..... }
非常感谢您的回复,我试了一下,提示”无法将string[]隐式转换为XXX.XXX .server1. Product[]",是不是还有其他地方要改。麻烦您再帮我看一下。[/quote] 函数里当然要返回Product的数组而不是string[]了。估计你要从selectAllCargoInfor这个函数改起,返回Product[].[/quote]
public class Product
        {
            private string M_ProdDesc; //产品描述
            private int M_Stock; //产品数量
            //创建Product对象
            public Product(string ProdDesc, int Stock)
            {
                this.M_ProdDesc = ProdDesc;
                this.M_Stock = Stock;
            }
            //姓名
            public string ProdDesc
            {
                get { return M_ProdDesc; }
            }
            //年龄
            public int Stock
            {
                get { return M_Stock; }
            }
        }
怎么把select出的数据放在数组里??
宝_爸 2014-11-17
  • 打赏
  • 举报
回复
引用 2 楼 wmgcn 的回复:
[quote=引用 1 楼 findcaiyzh 的回复:] 你放回的就是string数组,我的觉得没有问题啊。你要有格式需要定义自己的类。 例如 class Product { public string ProdDesc {get;set;} public int Stock{get;set;} } [WebMethod(Description = "获取所有货物的信息")] public Product[] selectAllCargoInfor() { ..... }
非常感谢您的回复,我试了一下,提示”无法将string[]隐式转换为XXX.XXX .server1. Product[]",是不是还有其他地方要改。麻烦您再帮我看一下。[/quote] 函数里当然要返回Product的数组而不是string[]了。估计你要从selectAllCargoInfor这个函数改起,返回Product[].
天下如山 2014-11-17
  • 打赏
  • 举报
回复
IList<string>------->IList<Product> Product自己定义.

12,163

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧