Click here to Skip to main content
15,884,099 members
Home / Discussions / C#
   

C#

 
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 
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 
OK this is what I have now and it seems to be working. A couple of questions/issues:

1. If you look at the commented out code below with the .ToString, I have PickupStoreInfo and then PickupDueDateDetails which is setup similar to the TrackingEventHistory and TrackingEventDetail, so would that be set up the same way with the Cast or should that be setup differently or is it ok like it is now.
C#
rspxml.Root.Add(new XElement("PickupStoreInfo"));
                    //    rspxml.Root.Add(new XElement("PickupDueDateDetails"));
                    //    rspxml.Root.Element("PickupDueDateDetails").Add(new XElement("Date", prc.History.ToString()));
                    //    rspxml.Root.Element("PickupDueDateDetails").Add(new XElement("UTCOffset", prc.History.ToString()));



2. You mentioned about using one of either XDocument or XmlDocument, I tried this and when I change to XDocument on this line:
C#
XmlDocument doc = new XmlDocument();

I get errors 'XDocument' does not contain a definition for 'XmlResolver' and no extension method 'XmlResolver' accepting a first argument of type 'XDocument' could be found (are you missing a using directive or an assembly reference?).

Same error definition for FirstChild, LoadXML.

If I change everything to XmlDocument like on this line from XDocument to XmlDocument:
C#
rspxml = XDocument.Parse

I get 'XmlDocument does not contain definition for 'Parse'.

3.Is all of my syntax correct, specifically all of the parentheses after this line:
C#
new XElement("CountryCode", prc.History)


I really appreciate all of your help and look forward to your response.

Here is the full code for this method:

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", 
        prc.History.Cast<Saia.Data.General.Shipment.HistoryItem>().Select(item => 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)
         )
         )
         )
         )
         )
         )
         )
         )
         );
         
         

   

                    //rspxml.Root.Add(new XElement("PackageTrackingInfo"));
                    //rspxml.Root.Element("PackageTrackingInfo").Add(new XElement("TrackingNumber", prc.ProNumber.ToString()));
                    //rspxml.Root.Add(new XElement("PackageDestinationLocation"));
                    //rspxml.Root.Element("PackageDestinationLocation").Add(new XElement("City", prc.Consignee.ToString()));
                    //rspxml.Root.Element("PackageDestinationLocation").Add(new XElement("StateProvince", prc.Consignee.ToString()));
                    //rspxml.Root.Element("PackageDestinationLocation").Add(new XElement("PostalCode", prc.Consignee.ToString()));
                    //rspxml.Root.Element("PackageDestinationLocation").Add(new XElement("CountryCode", prc.Consignee.ToString()));
                    //rspxml.Root.Add(new XElement("PackageDeliveryDate"));
                    //rspxml.Root.Element("PackageDeliveryDate").Add(new XElement("ScheduledDeliveryDate", prc.Consignee.ToString()));
                    //rspxml.Root.Element("PackageDeliveryDate").Add(new XElement("ReScheduledDeliveryDate", prc.Consignee.ToString()));
                    //rspxml.Root.Add(new XElement("TrackingEventHistory"));
                    //foreach (Saia.Data.General.Shipment.HistoryItem itm in prc.History)
                    //{
                    //    rspxml.Root.Add(new XElement("TrackingEventDetail"));
                    //    rspxml.Root.Element("TrackingEventDetail").Add(new XElement("EventStatus", prc.History.ToString()));
                    //    rspxml.Root.Element("TrackingEventDetail").Add(new XElement("EventReason", prc.History.ToString()));
                    //    rspxml.Root.Element("TrackingEventDetail").Add(new XElement("EventDateTime", prc.History.ToString()));
                    //    rspxml.Root.Add(new XElement("EventLocation"));
                    //    rspxml.Root.Element("EventLocation").Add(new XElement("Street1", prc.History.ToString()));
                    //    rspxml.Root.Element("EventLocation").Add(new XElement("City", prc.History.ToString()));
                    //    rspxml.Root.Element("EventLocation").Add(new XElement("StateProvince", prc.History.ToString()));
                    //    rspxml.Root.Element("EventLocation").Add(new XElement("PostalCode", prc.History.ToString()));
                    //    rspxml.Root.Element("EventLocation").Add(new XElement("CountryCode", prc.History.ToString()));
                    //    rspxml.Root.Element("TrackingEventDetail").Add(new XElement("SignedForByName", prc.History.ToString()));
                    //    rspxml.Root.Add(new XElement("PickupStoreInfo"));
                    //    rspxml.Root.Add(new XElement("PickupDueDateDetails"));
                    //    rspxml.Root.Element("PickupDueDateDetails").Add(new XElement("Date", prc.History.ToString()));
                    //    rspxml.Root.Element("PickupDueDateDetails").Add(new XElement("UTCOffset", prc.History.ToString()));
                    //    rspxml.Root.Add(new XElement("PickupID"));
                    //    rspxml.Root.Add(new XElement("StoreName"));
                    //    rspxml.Root.Add(new XElement("LocationID"));
                    //    rspxml.Root.Add(new XElement("StoreLocation"));
                    //    rspxml.Root.Element("StoreLocation").Add(new XElement("Street1", prc.History.ToString()));
                    //    rspxml.Root.Element("StoreLocation").Add(new XElement("City", prc.History.ToString()));
                    //    rspxml.Root.Element("StoreLocation").Add(new XElement("StateProvince", prc.History.ToString()));
                    //    rspxml.Root.Element("StoreLocation").Add(new XElement("PostalCode", prc.History.ToString()));
                    //    rspxml.Root.Element("StoreLocation").Add(new XElement("CountryCode", prc.History.ToString()));

                    }


                
            }

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

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 
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
Bootzilla3319-Sep-17 7:28
Bootzilla3319-Sep-17 7:28 
QuestionXSL to get only elements which have no childs Pin
MrKBA18-Sep-17 23:27
MrKBA18-Sep-17 23:27 

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.