2013-11-25 2 views
1

WebService 내에서 CSV 파일을 사용하여 사전을 채우는 데 도움이 필요하지만 결과를 반환 할 수 없습니다.webservice를 사용하여 사전을 반환하십시오.

Im은 사전을 두 개의 분리 된 열로 구분하여 작동하지만 외관상으로는 작동하지만 두 개가 아닌 값을 반환 할 수는 없습니다. 여기

는 코드입니다.

{ 
    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() 

잘못된 서명을 반환하고 있습니다. 실제 사전을 반환해야합니다 :

또한210

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; 

사실, 웹 메소드에서 실제 사전 객체를 반환하는 것은별로 의미가 없습니다. 정말로 원하는 것은 XML을 직렬화 한 사전 또는 목록입니다. 자세한 내용은 여기 : https://www.google.com/search?q=return+dictionary+in+web+method을 참조하십시오.

+0

대단히 감사합니다. 매우 도움이되며 해결됩니다. 웹 서비스가 사전 클래스를 받아들이지 않기 때문에 당신이 적절하게 말하지 않더라도. 직렬화 된 XML 사전을 살펴보고 거기에서 시작하겠습니다. 다시 감사합니다. – user3034104

관련 문제