Export (0) Print
Expand All

How to Process Errors In RMT Ads in C#

The following C# sample shows how to use the Creative Management API to parse an XML error structure returned while creating a Rich Media Tracker ad.

public static void CreateAdWithXmlErrors(
            string username,
            string password,
            string devToken,
            Guid advertiserId)
{
    // Create three AdExit objects. The first and the third objects
    // are invalid and will generate errors. This sample will
    // demonstrate how to parse the errors returned.
    var adExits = new AdExit[3] { 
        new AdExit { ClickThroughUrl = "notValidUrl",
            Description = "will generate error" },
        new AdExit { ClickThroughUrl = "http://someurl.com", 
            Description = "desc" },
        new AdExit { ClickThroughUrl = "anotherUrlNotValid", 
            Description = "will generate error" },
    };

    // Create a request object for CreateAd.
    var request = new CreateAdRequest
    {
        Ad = new RichMediaTrackerAd
        {
            CreativeDescription = "creativeDescription",
            AdTag = "adTag",
            DeliveryMethod = DeliveryMethodType.IFrame,
            AdExits = adExits,
            AdEvents = new AdEvent[] { },
            Width = 300,
            Height = 250
        },
                
        AdvertiserId = advertiserId,
        UserName = username,
        Password = password,
        DeveloperToken = devToken
    };

    try
    {
        // Create the client that will communicate
        // with the web service.
        CreativeServiceClient client = new CreativeServiceClient();

        // Call CreateAd, which will generate errors
        // because of the invalid URLs in the AdExit objects.
        client.CreateAd(request);
    }
    catch (FaultException<AgencyServicesFault> faultException)
    {
        string errorCode = faultException.Detail.OperationErrors[0].Code;

        // Having a Code of CreativeServicePropertyArrayInvalid indicates
        // that there will be further error information encoded in XML.
        if (errorCode == "CreativeServicePropertyArrayInvalid")
        {
            try
            {
                // Create an XmlDocument to parse the error structure.
                XmlDocument xmlDoc = new XmlDocument();

                // Now parse the XML.
                xmlDoc.LoadXml(
                    faultException.Detail.OperationErrors[0].Details);

                // Error nodes were found in child nodes below the top node.
                foreach (XmlNode n in xmlDoc.FirstChild.ChildNodes)
                {
                    var index = n.Attributes["Index"].Value;
                    var code = n.Attributes["Code"].Value.Trim();
                    Console.WriteLine("Index: {0} ErrorCode: {1}", 
                        index, code);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error parsing error XML: " + e.Message);
            }

        }
    }
}


Show:
© 2014 Microsoft