Skip to main content
Published: December 21 2010, 4:24:00 PMUpdated: November 28 2020, 9:06:20 PM

 

How to de-serialize response payload received using .NET framework.

 

  1. Add a reference to the eBay.Service.dll
  2. Read the SOAP payload received from eBay in a XML document.  
  3. Using System.Xml.Serialization we can de-seralize the payload into an object for storing of display purpose.
  4. For example, consider the GetItemTransactions SOAP payload from eBay.

The following is the code sample for converting the payload to an object using C#.NET

Code in the Program.cs file

 

private void button1_Click(object sender, EventArgs e)

{

     try

     {

        XmlDocument xmlDoc = new XmlDocument();

        xmlDoc.LoadXml(textBox1.Text.Trim());

        XmlNode nodeList = xmlDoc.SelectSingleNode(@".//*[local-name(.)='GetItemTransactionsResponse']");

        nodeList.Attributes.RemoveAll();

        string xmlStr = nodeList.OuterXml.Replace("GetItemTransactionsResponse", "GetItemTransactionsResponseType");

        GetItemTransactionsResponseType res = (GetItemTransactionsResponseType)DeserializeObject(xmlStr);

        label2.Text = "ItemID: " + res.Item.ItemID;

        label3.Text = "Title: " + res.Item.Title;

     }

     catch (Exception ex)

     {

        throw ex;

     }

}

private object DeserializeObject(string xmlStr)

{

     GetItemTransactionsResponseType responseObj = new GetItemTransactionsResponseType();

     StringReader read = new StringReader(xmlStr);

     XmlSerializer serializer = new XmlSerializer(responseObj.GetType(), strNamespace);

     XmlReader reader = new XmlTextReader(read);

     try

     {

        responseObj = (GetItemTransactionsResponseType)serializer.Deserialize(reader);

        return responseObj;

     }

     catch (Exception ex)

     {

        throw ex;

     }

     finally

     {

        reader.Close();

        read.Close();

        read.Dispose();

    }

}

 

Note:

Please see the attachment for a sample windows application which converts GetItemTransactions SOAP payload to a GetItemTransactionsResponseType object.

How well did this answer your question?
Answers others found helpful