2013-04-05 90 views
0

我是REST webservices的新手。我需要在C#控制檯應用程序中使用現有的REST服務。我正在獲取以下行中的XML響應。如何從客戶端讀取StreamReader響應?

readStream.ReadLine(); 

我們如何能夠利用客戶端的REST的反應呢?

實用

public void SearchContactDetailsAsync(Models.AddressBookRequest addressBookDataRequest) 
    { 
     UriBuilder builder = new UriBuilder(url); 
     restClient.DoPost(builder.Uri, Serializer.SerializeXml(addressBookDataRequest.contactsSearchCriteria), SearchContactSuccess, SearchContactFailed, addressBookDataRequest.HeaderParams); 
    } 

    private void SearchContactSuccess(HttpWebResponse response) 
    { 
     //Call base service method - to inspect the response and publish an event 
     HandleServiceSearchSuccess<ContactDetailsPreview[]>(SearchContactDetailsCompleted, "contactDetailsPreviews", response); 
     Stream receiveStream = response.GetResponseStream(); 
     Encoding encode = System.Text.Encoding.UTF8; 

     StreamReader readStream = new StreamReader(receiveStream, encode); 
     readStream.ReadLine(); 

    } 

控制檯應用程序

public void MyMethod() 
    { 
     autoRestEvent = new AutoResetEvent(false); 
     services = new communicationSvcs(); 
     services.SearchContactDetailsCompleted += new EventHandler<RestClientUtility.EventArg.ServiceResponseEventArgs<RestClientUtility.Models.ContactDetailsPreview[]>>(services_SearchContactDetailsCompleted); 

     //Call the operation 
     AddressBookRequest req = new AddressBookRequest 
     { 
      contactsSearchCriteria = new ContactsSearchCriteria 
      { 
       searchUserID = "ss23ed" 

      }, 
      HeaderParams = new RestClientUtility.Requests.HttpHeaderParms 
      { 
       UserId = "ss23ed", 
       UserPrincipalName = " [email protected]", 
       ContentType = "application/xml" 
      } 
     }; 
     services.SearchContactDetailsAsync(req); 
     autoRestEvent.WaitOne(); 

    } 

參考

  1. XML deserialization generic method

回答

1

以下是用於讀取來自REST服務的響應的僞代碼。這具有一個通用的反序列化方法

注:ContactDetailsPreview是響應對象類型

//通用反序列化

public static T DeserializeXml<T>(Stream stream, XmlRootAttribute root) 
    { 
      XmlSerializer deserializer = new XmlSerializer(typeof(T), root); 
      return (T)deserializer.Deserialize(stream); 
    } 

//代碼段1

restClient.DoPost(builder.Uri, Serializer.SerializeXml(addressBookDataRequest.contactsSearchCriteria), SearchContactSuccess, SearchContactFailed, addressBookDataRequest.HeaderParams); 

//代碼段2

public event EventHandler<ServiceResponseEventArgs<ContactDetailsPreview[]>> SearchContactDetailsCompleted; 

//代碼段3

private void SearchContactSuccess(HttpWebResponse response) 
    { 

     //Call base service method - to inspect the response and publish an event 
     HandleServiceSearchSuccess<ContactDetailsPreview[]>(SearchContactDetailsCompleted, "contactDetailsPreviews", response); 
     Stream receiveStream = response.GetResponseStream(); 
     Encoding encode = System.Text.Encoding.UTF8; 
     StreamReader readStream = new StreamReader(receiveStream, encode); 
     readStream.ReadLine(); 
    } 

//通用處理的搜索成功響應

public void HandleServiceSearchSuccess<T>(EventHandler<ServiceResponseEventArgs<T>> eventHandler, String rootElementName, HttpWebResponse response) 
{ 
EventHandler<ServiceResponseEventArgs<T>> theGivenEventHandler = eventHandler; 
obj = Serializer.DeserializeXml<T>(response.GetResponseStream(), new XmlRootAttribute() { ElementName = rootElementName }); 
    theGivenEventHandler(this, new ServiceResponseEventArgs<T>(obj)); 

} 
+0

參考:http://stackoverflow.com/questions/14562415/xml-deserialization-generic-method – Lijo 2013-04-26 11:47:03

1

.NET的XmlDocument Class具有接受流

當我看到它,你只需要提供流給它

XmlDocument doc = XmlDocument.Load(readStream); 

我真的不能看到load()方法如果它是一個靜態方法,我沒有環境在這裏測試它,但也許你需要先創建一個XmlDocument實例,然後從中運行Load()方法(如果它不是靜態的)

+0

通過'使用的System.Xml;'或(如果沒有參考)添加一個新的參考系統.Xml庫 – Torben 2013-04-05 11:23:35

1

要創建XmlDocument fr OM流: -

XmlDocument document = new XmlDocument(); 
using(StreamReader readStream = new StreamReader(receiveStream, encode)) 
{   
    document.Load(readStream); 
} 

重寫MyMethod採取的XmlDocument

public void MyMethod(XmlDocument xDoc) 

而在它傳遞的SearchContactSuccess方法裏面,假設你有一個類的實例,併到哪裏參考consoleapp/utility駐留等:

MyMethod(document); 
相關問題