Developers Program

How to deserialize email notifications into objects with .NET

Published: July 06 2006, 11:23:00 AMยทUpdated: July 29 2022, 3:52:36 AM

Products

Question

I receive notifications via email and need to deserialize it into objects. How can I do this with .NET?

Answer

I receive notifications via email and need to deserialize it into objects. How can I do this with .NET?

The content of the email notification is equivalent to the response that you would get when you make a corresponding SOAP request. For instance, if you get a notification for FixedPriceTransaction, the payload is similar to GetItemTransactions with the detail level ReturnAll. You can get a list of notification payloads from the documentation.

The easiest way to deserialize the payload into an object is to apply an xsl transformation to extract the child from the soapenv:Body element and deserialize it to an appropriate object. Lets take the example of the FixedPriceTransaction notification.

Here is a sample .NET code to deserialize it into a GetItemTransactionsResponseType object:

using System;
using eBay.Service.Call;
using eBay.Service.Core.Sdk;
using eBay.Service.Util;
using eBay.Service.Core.Soap;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Xsl;

namespace SDK3Sample
{
private void Deserialize(string infile, string outfile, string xslfile)
{
//infile: file containing the notification payload
//outfile: this will contain the xml after the transformation is applied
//xslfile: the file containing the xsl for transformation
XmlSerializer oDeSerializer = new XmlSerializer(typeof(GetItemTransactionsResponseType));
XmlUrlResolver resolver = new XmlUrlResolver();

//Load and transform the xml
XslTransform xslTransform = new XslTransform();
xslTransform.Load(xslfile);
xslTransform.Transform(infile, outfile, resolver);

//Read the transformed xml
FileStream oFileStream = new FileStream(outfile, FileMode.Open);

// Declare an object variable of the type to be deserialized.
GetItemTransactionsResponseType tranResponse = (GetItemTransactionsResponseType) oDeSerializer.Deserialize(oFileStream);
}
}

Here is the content of the xslfile:
--------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:eBay="urn:ebay:apis:eBLBaseComponents">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<GetItemTransactionsResponseType>
<xsl:copy-of select="//eBay:GetItemTransactionsResponse/*"/>
</GetItemTransactionsResponseType>
</xsl:template>
</xsl:stylesheet>

How well did this answer your question?