Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to handle multiple exceptions(Try..Catch)

How to handle multiple exceptions(Try..Catch)

Scheduled Pinned Locked Moved C#
sysadmindebuggingxmljsonhelp
26 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Bootzilla33

    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.

    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:

    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:

    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:

    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:

    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;
    
    Richard DeemingR Offline
    Richard DeemingR Offline
    Richard Deeming
    wrote on last edited by
    #17

    1) It's not clear what you're trying to do here. You're not adding an element for each history item, so you don't need the Select; you just need to pass in the correct values to the elements.

    rspxml.Root.Add(new XElement("PickupDueDateDetails"),
    new XElement("Date", prc.PickupDueDate),
    new XElement("UTCOffset", prc.PickupDueDateOffset)
    );

    2) You can't just change the type and expect the existing code to work. You need to fix the existing code to use the correct methods for the type. For example, this:

    XmlDocument doc = new XmlDocument();
    doc.XmlResolver = null;
    doc.LoadXml(xmlRequest);

    would become:

    XDocument doc = XDocument.Parse(xmlResult);

    This:

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

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

    would become:

    XElement mainNode = doc.Root;
    string method = mainNode.Name.LocalName;
    // No need to check for the processing instruction here...

    This:

    string pronum = mainNode.SelectSingleNode("TrackingNumber").InnerText;

    would become:

    string pronum = (string)mainNode.Element("TrackingNumber");

    3) I don't know. Does it compile? Does it do what you expect it to do? I very much doubt that prc.History is the correct property to use for all of those elements.


    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

    B 1 Reply Last reply
    0
    • Richard DeemingR Richard Deeming

      1) It's not clear what you're trying to do here. You're not adding an element for each history item, so you don't need the Select; you just need to pass in the correct values to the elements.

      rspxml.Root.Add(new XElement("PickupDueDateDetails"),
      new XElement("Date", prc.PickupDueDate),
      new XElement("UTCOffset", prc.PickupDueDateOffset)
      );

      2) You can't just change the type and expect the existing code to work. You need to fix the existing code to use the correct methods for the type. For example, this:

      XmlDocument doc = new XmlDocument();
      doc.XmlResolver = null;
      doc.LoadXml(xmlRequest);

      would become:

      XDocument doc = XDocument.Parse(xmlResult);

      This:

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

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

      would become:

      XElement mainNode = doc.Root;
      string method = mainNode.Name.LocalName;
      // No need to check for the processing instruction here...

      This:

      string pronum = mainNode.SelectSingleNode("TrackingNumber").InnerText;

      would become:

      string pronum = (string)mainNode.Element("TrackingNumber");

      3) I don't know. Does it compile? Does it do what you expect it to do? I very much doubt that prc.History is the correct property to use for all of those elements.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      B Offline
      B Offline
      Bootzilla33
      wrote on last edited by
      #18
      1. This may help explain. This is some of the XML I am trying to write out:

      LK
      AQ
      2004-08-22T11:00:00-
      08:00
      SEATTLE
      WA
      98107
      US
      JOHN GALT

      2004-08-25
      -07:00
      92253
      US-PRI-DEL-03
      19632
      NW Market St
      SEATTLE
      WA
      98107
      US

      Am I doing a rspxml.Root.Add for each section or can I use like this:

      new XElement("PickupStoreInfo",
      new XElement("PickupDueDateDetails",
      new XElement("Date", prc.History),
      new XElement("UTCOffset", prc.History)
      ),

      So based off this, how would that be written out in the code. 2) I updated the code and that seems to be working ok. 3) It does compile. And yes those are supposed to be prc.History. There are only two elements/fields that I'm concerned with and that is the Consignee and History so yes they should be prc.History. I just need to pull in the fields for in those two areas.

      Richard DeemingR 1 Reply Last reply
      0
      • B Bootzilla33
        1. This may help explain. This is some of the XML I am trying to write out:

        LK
        AQ
        2004-08-22T11:00:00-
        08:00
        SEATTLE
        WA
        98107
        US
        JOHN GALT

        2004-08-25
        -07:00
        92253
        US-PRI-DEL-03
        19632
        NW Market St
        SEATTLE
        WA
        98107
        US

        Am I doing a rspxml.Root.Add for each section or can I use like this:

        new XElement("PickupStoreInfo",
        new XElement("PickupDueDateDetails",
        new XElement("Date", prc.History),
        new XElement("UTCOffset", prc.History)
        ),

        So based off this, how would that be written out in the code. 2) I updated the code and that seems to be working ok. 3) It does compile. And yes those are supposed to be prc.History. There are only two elements/fields that I'm concerned with and that is the Consignee and History so yes they should be prc.History. I just need to pull in the fields for in those two areas.

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #19

        Bootzilla33 wrote:

        new XElement("PickupStoreInfo",
        new XElement("PickupDueDateDetails",
        new XElement("Date", prc.History),
        new XElement("UTCOffset", prc.History)
        ),

        Since PickupDueDateDetails is a child of PickupStoreInfo, that's the correct way to do it. But passing prc.History as the value for every node isn't going to produce the correct values in the resulting XML. Instead, you'll get the result of calling prc.History.ToString() inserted into every node. If you don't need to pass a value in the node, it would be better to not pass a value to it, or exclude it from the document.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        B 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          Bootzilla33 wrote:

          new XElement("PickupStoreInfo",
          new XElement("PickupDueDateDetails",
          new XElement("Date", prc.History),
          new XElement("UTCOffset", prc.History)
          ),

          Since PickupDueDateDetails is a child of PickupStoreInfo, that's the correct way to do it. But passing prc.History as the value for every node isn't going to produce the correct values in the resulting XML. Instead, you'll get the result of calling prc.History.ToString() inserted into every node. If you don't need to pass a value in the node, it would be better to not pass a value to it, or exclude it from the document.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          B Offline
          B Offline
          Bootzilla33
          wrote on last edited by
          #20

          I have the same thing for prc.Consignee. So what would you suggest something like this:

          ),
          new XElement("PickupStoreInfo",
          new XElement("PickupDueDateDetails",
          new XElement("Date"),
          new XElement("UTCOffset")
          ),

          I pretty much have to pass a value in the node or else how else would I get the values?

          Richard DeemingR 1 Reply Last reply
          0
          • B Bootzilla33

            I have the same thing for prc.Consignee. So what would you suggest something like this:

            ),
            new XElement("PickupStoreInfo",
            new XElement("PickupDueDateDetails",
            new XElement("Date"),
            new XElement("UTCOffset")
            ),

            I pretty much have to pass a value in the node or else how else would I get the values?

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #21

            If you have to pass a value, then pass the value that the API is expecting. Passing the same (invalid) string for each node will most likely cause the API to return an error.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            B 1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              If you have to pass a value, then pass the value that the API is expecting. Passing the same (invalid) string for each node will most likely cause the API to return an error.


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              B Offline
              B Offline
              Bootzilla33
              wrote on last edited by
              #22

              Richard Deeming wrote:

              If you have to pass a value, then pass the value that the API is expecting.

              What would that be? Something like:

              new XElement("PickupStoreInfo",
              new XElement("PickupDueDateDetails",
              new XElement("Date", Date),
              new XElement("UTCOffset", UTCOffset)
              ),

              Not sure???

              Richard DeemingR 1 Reply Last reply
              0
              • B Bootzilla33

                Richard Deeming wrote:

                If you have to pass a value, then pass the value that the API is expecting.

                What would that be? Something like:

                new XElement("PickupStoreInfo",
                new XElement("PickupDueDateDetails",
                new XElement("Date", Date),
                new XElement("UTCOffset", UTCOffset)
                ),

                Not sure???

                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #23

                Pass in the values that the API is expecting you to pass in for the request. You've presumably read the API documentation? The expected values should be documented there.


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                B 1 Reply Last reply
                0
                • Richard DeemingR Richard Deeming

                  Pass in the values that the API is expecting you to pass in for the request. You've presumably read the API documentation? The expected values should be documented there.


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  B Offline
                  B Offline
                  Bootzilla33
                  wrote on last edited by
                  #24

                  Yes i've read it and if that is the case then the values in the API are the same as the XElement so Here the a piece of the API schema:

                  Event Status

                  I guess it will look like this?

                          new XElement("EventStatus", "Event Status"),
                          
                      ),
                  
                  B 1 Reply Last reply
                  0
                  • B Bootzilla33

                    Yes i've read it and if that is the case then the values in the API are the same as the XElement so Here the a piece of the API schema:

                    Event Status

                    I guess it will look like this?

                            new XElement("EventStatus", "Event Status"),
                            
                        ),
                    
                    B Offline
                    B Offline
                    Bootzilla33
                    wrote on last edited by
                    #25

                    Bootzilla33 wrote:

                    Here the a piece of the API schema:

                    <xsd:element
                    name="EventStatus" type="xsd:string">
                    xsd:annotation\
                    xsd:documentation\Event Status</xsd:documentation>
                    </xsd:annotation>
                    </xsd:element>

                    I guess it will look like this?

                    new XElement("EventStatus", "Event Status"),
                    

                    ),

                    Just need help with this and I think I'm done.

                    B 1 Reply Last reply
                    0
                    • B Bootzilla33

                      Bootzilla33 wrote:

                      Here the a piece of the API schema:

                      <xsd:element
                      name="EventStatus" type="xsd:string">
                      xsd:annotation\
                      xsd:documentation\Event Status</xsd:documentation>
                      </xsd:annotation>
                      </xsd:element>

                      I guess it will look like this?

                      new XElement("EventStatus", "Event Status"),
                      

                      ),

                      Just need help with this and I think I'm done.

                      B Offline
                      B Offline
                      Bootzilla33
                      wrote on last edited by
                      #26

                      Someone please help me with this. I would greatly appreciate it. Thanks

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups