2013-11-25 74 views
1

我正在寻找一些帮助,使用WebService中的csv文件填充字典,但是我无法返回结果。使用webservice返回字典

为了将字典分成两个看似有效的单独列,但由于有两个而不是一个,所以我不能返回值。

这里是代码:

{ 
    StreamReader streamReader = new StreamReader("T:/4 Year WBL/Applications Development/Coursework 2/2b/Coursework2bwebservice/abrev.csv"); 

    [WebMethod] 
    public string Dictionary() 
    { 
     string line; 
     Dictionary<string, string> dictionary = new Dictionary<string,string>(); 
     while ((line = streamReader.ReadLine()) !=null) 
     { 
     string[] columns = line.Split(','); 
     dictionary.Add(columns[0], columns[1]); 

     return dictionary;  
     } 
    } 

我收到错误“不能隐式转换类型System.Collections.Generic.Dictionary<string,string to string>"

任何想法将是巨大的,感谢您的时间

回答

0
public string Dictionary() 

返回错误的签名您需要返回一个实际的字典:

public Dictionary<string, string> Dictionary() 

此外,这

while ((line = streamReader.ReadLine()) !=null) 

似乎有点hinky。你也在回路中返回你的dictionary。让我们来试试吧:

line = streamReader.Readline(); 
while (line !=null) 
{ 
    string[] columns = line.Split(','); 
    dictionary.Add(columns[0], columns[1]); 
    line = streamReader.Readline(); 
} 
return dictionary; 

所有这一切说,返回一个实际的字典对象在Web方法可能没有多大意义。你真正想要的是一个XML序列化的字典或列表。请参阅:https://www.google.com/search?q=return+dictionary+in+web+method了解更多信息。

+0

非常感谢,这是非常有益的和解决。虽然如你所说不正确,因为Web服务不接受字典类。我会看看序列化的XML字典,并从那里开始。再次感谢你。 – user3034104