1

I am new to REST webservices. There is an existing REST service that I need to consume in a C# console application. I am getting the XML response in the following line.

readStream.ReadLine();

How can we make use of the REST response in the client?

Utility

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

    }

Console App

    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 = " ss23ed@hotmail.com",
                ContentType = "application/xml"
            }
        };
        services.SearchContactDetailsAsync(req);
        autoRestEvent.WaitOne();

    }

References

  1. XML deserialization generic method
Community
  • 1
  • 1
LCJ
  • 22,196
  • 67
  • 260
  • 418

3 Answers3

1

.NET's XmlDocument Class has a Load() Method that accepts a stream

As I see it, you only need to provide the stream to it

 XmlDocument doc = XmlDocument.Load( readStream );

I can't really see if it's a static method and I have no environment to test it right here, but maybe you need to create an instance of XmlDocument first and then run the Load() method from it (if it's not static)

Torben
  • 193
  • 11
  • Through `using System.Xml;` or (if there's no reference) add a new reference to the System.Xml library – Torben Apr 05 '13 at 11:23
1

To create an XmlDocument from a stream: -

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

Rewrite MyMethod to take an XmlDocument:

public void MyMethod(XmlDocument xDoc)

And pass it in, inside of the SearchContactSuccess method, assuming you have an instance of the class and a reference to wherever the consoleapp/utility resides etc:

MyMethod(document);
DGibbs
  • 14,316
  • 7
  • 44
  • 83
1

Following is the pseudo-code for reading response from REST Service. This has a generic deserialization approach

Note: ContactDetailsPreview is the response object type

//Generic Deserialization

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

// Code Segment 1

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

//Code Segment 2

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

//Code Segment 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();
    }

//Generic handler for Search Success response

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));

}
LCJ
  • 22,196
  • 67
  • 260
  • 418
  • References: http://stackoverflow.com/questions/14562415/xml-deserialization-generic-method – LCJ Apr 26 '13 at 11:47