2013-11-25 127 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