Click here to Skip to main content
15,887,746 members
Home / Discussions / C#
   

C#

 
QuestionHow to handle multiple exceptions(Try..Catch) Pin
Bootzilla3319-Sep-17 4:55
Bootzilla3319-Sep-17 4:55 
SuggestionRe: How to handle multiple exceptions(Try..Catch) Pin
Richard Deeming19-Sep-17 5:22
mveRichard Deeming19-Sep-17 5:22 
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
Bootzilla3319-Sep-17 5:32
Bootzilla3319-Sep-17 5:32 
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
Richard Deeming19-Sep-17 5:57
mveRichard Deeming19-Sep-17 5:57 
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
Bootzilla3319-Sep-17 6:05
Bootzilla3319-Sep-17 6:05 
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
Bootzilla3319-Sep-17 9:58
Bootzilla3319-Sep-17 9:58 
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
eddieangel19-Sep-17 12:18
eddieangel19-Sep-17 12:18 
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
Bootzilla3320-Sep-17 2:56
Bootzilla3320-Sep-17 2:56 
I already have these

using System.Linq;
using System.Xml.Linq;

You mentioned Cast to something else but not sure what to do there. Anyone else with any suggestions would be appreciated.If no one has any suggestions then I will have to go back to the ToString code I had previously and just go with that.
This is what I have now:
C#
 public string ProcessXML(string xmlRequest)
        {
            XDocument rspxml = null;
            try
            {
                if (bool.Parse(WebConfigurationManager.AppSettings["Debug"]) == true)
                    File.WriteAllText(Path.Combine(Server.MapPath("Log"), DateTime.Now.ToString("MMddyyy_HHmmss") + ".xml"), xmlRequest);
                // Determine Method to Call
                XmlDocument doc = new XmlDocument();
                doc.XmlResolver = null;
                doc.LoadXml(xmlRequest);

                string method = doc.FirstChild.Name;
                XmlNode mainNode = doc.FirstChild;

                if (method.ToLower() == "xml")
                {
                    method = doc.FirstChild.NextSibling.Name;
                    mainNode = doc.FirstChild.NextSibling;
                }

                if (method == "AmazonTrackingRequest")
                {
                    Saia.Data.General.Shipment prc = new Data.General.Shipment();
                    string pronum = mainNode.SelectSingleNode("TrackingNumber").InnerText;
                    prc.GetByProNumber(decimal.Parse(pronum));

                    if (string.IsNullOrEmpty(pronum))
                    {
                        rspxml = XDocument.Parse("<?xml version=\"1.0\" encoding=\"UTF - 8\"?><AmazonTrackingResponse xmlns: xsi = 'http://www.w3.org/2001/XMLSchema-instance'xsi: noNamespaceSchemaLocation='AmazonTrackingResponse.xsd'><APIVersion >4.0</APIVersion><TrackingErrorInfo><TrackingNumber>12345678</TrackingNumber><TrackingErrorDetail><ErrorDetailCode>ERROR_101</ErrorDetailCode><ErrorDetailCodeDesc>INVALID TRACKING NUMBER</ErrorDetailCodeDesc></TrackingErrorDetail></TrackingErrorInfo></AmazonTrackingResponse>");
                    }
                    else
                    {
                        decimal tn = 0.0m;

                        if (decimal.TryParse(pronum, out tn))

                        {
                            prc.GetByProNumber(tn);
                        }
                        else
                        {
                            Console.WriteLine("Unable to parse '{0}'.", pronum);
                        }
                    }


                    rspxml = XDocument.Parse("<AmazonTrackingResponse xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='AmazonTrackingResponse.xsd'></AmazonTrackingResponse>");
                    rspxml.Root.Add(new XElement("APIVersion", "4.0"));

                    var errorInfo = rspxml.Descendants("TrackingErrorInfo").FirstOrDefault();
                    if (errorInfo != null)
                    {
                        string packtrackinginfo = (string)errorInfo.Element("PackageTrackingInfo");

                        var detail = errorInfo.Element("TrackingErrorDetail");
                        if (detail != null)
                        {
                            string errorCode = (string)detail.Element("ErrorDetailCode");
                            string errorMessage = (string)detail.Element("ErrorDetailCodeDesc");
                            // Do something with the error details...
                        }
                    }


        rspxml.Root.Add(
        new XElement("API", "4.0"),
        new XElement("PackageTrackingInfo",
            new XElement("TrackingNumber", prc.ProNumber)
        ),
        new XElement("PackageDestinationLocation",
            new XElement("City", prc.Consignee),
            new XElement("StateProvince", prc.Consignee),
            new XElement("PostalCode", prc.Consignee),
            new XElement("CountryCode", prc.Consignee)
        ),
        new XElement("PackageDeliveryDate",
            new XElement("ScheduledDeliveryDate", prc.Consignee),
            new XElement("ReScheduledDeliveryDate", prc.Consignee)
        ),
        new XElement("TrackingEventHistory",
            new XElement("TrackingEventDetail",
            new XElement("EventStatus", prc.History),
            new XElement("EventReason", prc.History),
            new XElement("EventDateTime", prc.History)

        ),
         new XElement("EventLocation",
            new XElement("Street1", prc.History),
            new XElement("City", prc.History),
            new XElement("StateProvince", prc.History),
            new XElement("PostalCode", prc.History),
            new XElement("CountryCode", prc.History),
            new XElement("SignedForByName", prc.History)
        ),
         new XElement("PickupStoreInfo",
         new XElement("PickupDueDateDetails",
            new XElement("Date", prc.History),
            new XElement("UTCOffset", prc.History)
        ),
         new XElement("PickupID",
         new XElement("StoreName",
         new XElement("LocationID",
         new XElement("StoreLocation",
            new XElement("Street1", prc.History),
            new XElement("City", prc.History),
            new XElement("StateProvince", prc.History),
            new XElement("PostalCode", prc.History),
            new XElement("CountryCode", prc.History)
         )
         )
         )
         )
         )
         )
         );
                 

                    }


                
            }

            
            catch (CodeException e)
            {
                rspxml = XDocument.Parse("<?xml version=\"1.0\" encoding=\"UTF - 8\"?><AmazonTrackingResponse xmlns: xsi = 'http://www.w3.org/2001/XMLSchema-instance'xsi: noNamespaceSchemaLocation='AmazonTrackingResponse.xsd'><APIVersion>4.0</APIVersion ><TrackingErrorInfo><TrackingNumber>12345678</TrackingNumber><TrackingErrorDetail><ErrorDetailCode>ERROR_203</ErrorDetailCode><ErrorDetailCodeDesc>XML DOCUMENT NOT WELL FORMED</ErrorDetailCodeDesc></TrackingErrorDetail></TrackingErrorInfo></AmazonTrackingResponse>");
                Core.Framework.Debug.CodeException(xmlRequest, e);
            }
            catch (Exception e)
            {
                rspxml = XDocument.Parse("<?xml version=\"1.0\" encoding=\"UTF - 8\"?><AmazonTrackingResponse xmlns: xsi = 'http://www.w3.org/2001/XMLSchema-instance'xsi: noNamespaceSchemaLocation='AmazonTrackingResponse.xsd'><APIVersion>4.0</APIVersion ><TrackingErrorInfo><TrackingNumber>12345678</TrackingNumber><TrackingErrorDetail><ErrorDetailCode>ERROR_203</ErrorDetailCode><ErrorDetailCodeDesc>XML DOCUMENT NOT WELL FORMED</ErrorDetailCodeDesc></TrackingErrorDetail></TrackingErrorInfo></AmazonTrackingResponse>");
                Core.Framework.Debug.Exception(xmlRequest, e);
            }

            return rspxml.ToString();
        }
        public class EventCode
        {
            public enum EvCodes {Delivered, DispatchedForDelivery, DepartedFromTerminal, ArrivedAtDestinationTerminal, DNF};
            public static void Main()
            {
                string val = "Dispatched For Delivery";
                switch ((EvCodes)Enum.Parse(typeof(EvCodes), val.Replace(" ", "")))
                {
                    case EvCodes.DispatchedForDelivery:
                    case EvCodes.Delivered:
                        Console.WriteLine("EVENT_301");
                        break;
                    case EvCodes.DepartedFromTerminal:
                        Console.WriteLine("EVENT_202");
                        break;
                    case EvCodes.ArrivedAtDestinationTerminal:
                        Console.WriteLine("EVENT_202");
                        break;
                    case EvCodes.DNF:
                        Console.WriteLine("EVENT_101");
                        Console.WriteLine("EVENT_102");
                        break;
                   
                    default:
                        break;
                }

            }
        }
    }
}


modified 20-Sep-17 9:27am.

GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
Richard Deeming20-Sep-17 10:01
mveRichard Deeming20-Sep-17 10:01 
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
Bootzilla3321-Sep-17 3:58
Bootzilla3321-Sep-17 3:58 
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
Richard Deeming21-Sep-17 4:32
mveRichard Deeming21-Sep-17 4:32 
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
Bootzilla3321-Sep-17 4:52
Bootzilla3321-Sep-17 4:52 
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
Richard Deeming21-Sep-17 5:18
mveRichard Deeming21-Sep-17 5:18 
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
Bootzilla3321-Sep-17 5:41
Bootzilla3321-Sep-17 5:41 
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
Richard Deeming21-Sep-17 6:07
mveRichard Deeming21-Sep-17 6:07 
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
Bootzilla3321-Sep-17 6:15
Bootzilla3321-Sep-17 6:15 
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
Richard Deeming21-Sep-17 6:19
mveRichard Deeming21-Sep-17 6:19 
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
Bootzilla3321-Sep-17 6:40
Bootzilla3321-Sep-17 6:40 
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
Bootzilla3321-Sep-17 18:19
Bootzilla3321-Sep-17 18:19 
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
Bootzilla3322-Sep-17 2:54
Bootzilla3322-Sep-17 2:54 
AnswerRe: How to handle multiple exceptions(Try..Catch) Pin
eddieangel19-Sep-17 6:07
eddieangel19-Sep-17 6:07 
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
Bootzilla3319-Sep-17 6:14
Bootzilla3319-Sep-17 6:14 
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
eddieangel19-Sep-17 6:30
eddieangel19-Sep-17 6:30 
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
Bootzilla3319-Sep-17 6:58
Bootzilla3319-Sep-17 6:58 
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
eddieangel19-Sep-17 7:17
eddieangel19-Sep-17 7:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.