Help with processing xml from an http api return

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mnvikefan

    #1

    Help with processing xml from an http api return

    What would be the best way to process a return from an http api login
    function that returns one of the following two XML streams?


    Successful login with a session ID returned

    <?xml version="1.0" encoding="UTF-8" ?>
    <CreateSessionR esponse>
    <result>0</result>
    <warning>Create Session succeeded</warning>
    <return>
    <SessionID>1126 765227212</SessionID>
    </return>
    </CreateSessionRe sponse>


    Unsuccessful return with a fault code and message

    <?xml version="1.0" encoding="UTF-8" ?>
    <CreateSessionR esponse>
    <fault>
    <faultcode>8000 4005</faultcode>
    <faultstring>Ag entIntegration error : Login failed. Reason is:
    Invalid User Name or Password. Please try again.</faultstring>
    </fault>
    </CreateSessionRe sponse>

  • Peter Bromberg [C# MVP]

    #2
    RE: Help with processing xml from an http api return

    You don't say if this is SOAP or just some sort of "plain XML" response
    structure.
    However, it looks like SOAP, but if you arent using a WebReference to the
    WSDL and a WebService Proxy, it's still easy to see that both responses
    represent a complete XmlDocument.

    So, to keep things simple, I'd load it into an XmlDocument, and look
    for the contents of the CreateSessionRe sponse node.
    if
    SelectSingleNod e("//CreateSessionRe sponse/fault/faultcode").Inn erText

    is not null, you got a SOAP FAULT.

    Conversely if

    SelectSingleNod e("//CreateSessionRe sponse/return/SessionID").Inn erText

    is not null, you got a Session.

    hope that makes sense.

    Peter

    --
    Co-founder, Eggheadcafe.com developer portal:

    UnBlog:





    "mnvikefan" wrote:
    [color=blue]
    > What would be the best way to process a return from an http api login
    > function that returns one of the following two XML streams?
    >
    >
    > Successful login with a session ID returned
    >
    > <?xml version="1.0" encoding="UTF-8" ?>
    > <CreateSessionR esponse>
    > <result>0</result>
    > <warning>Create Session succeeded</warning>
    > <return>
    > <SessionID>1126 765227212</SessionID>
    > </return>
    > </CreateSessionRe sponse>
    >
    >
    > Unsuccessful return with a fault code and message
    >
    > <?xml version="1.0" encoding="UTF-8" ?>
    > <CreateSessionR esponse>
    > <fault>
    > <faultcode>8000 4005</faultcode>
    > <faultstring>Ag entIntegration error : Login failed. Reason is:
    > Invalid User Name or Password. Please try again.</faultstring>
    > </fault>
    > </CreateSessionRe sponse>
    >
    >[/color]

    Comment

    • mnvikefan

      #3
      Re: Help with processing xml from an http api return

      These are not soap messages. There is no web reference to a wsdl.
      However, thankyou for the comments about loading the documents into an
      XmlDocument object and using the SelectSingleNod e method. I think that
      this will help me very much. I was wondering if I should use an
      XmlTextReader to get the data and while that would have worked, I don't
      think it is as straight forward as the method you just presented me.

      Comment

      • Peter Bromberg [C# MVP]

        #4
        Re: Help with processing xml from an http api return

        You can and should consider using an XmlTextReader, as it is more efficient.
        I was just trying to bring it down to a "lowest common denominator".
        Peter
        --
        Co-founder, Eggheadcafe.com developer portal:

        UnBlog:





        "mnvikefan" wrote:
        [color=blue]
        > These are not soap messages. There is no web reference to a wsdl.
        > However, thankyou for the comments about loading the documents into an
        > XmlDocument object and using the SelectSingleNod e method. I think that
        > this will help me very much. I was wondering if I should use an
        > XmlTextReader to get the data and while that would have worked, I don't
        > think it is as straight forward as the method you just presented me.
        >
        >[/color]

        Comment

        • GeekCIA

          #5
          Re: Help with processing xml from an http api return

          For the most part, your suggestion below is working, but now I am
          having a weird issue with the following XML return.

          <?xml version="1.0" encoding="UTF-8" ?>
          <ActivateWithTe xtResponse>
          <result>0</result>
          <warning>Activa teWithText succeeded</warning>
          <return>
          <NumberConcepts >33</NumberConcepts>
          </return>
          </ActivateWithTex tResponse>

          Here is the error segment that I am receiving...

          Exception Details: System.NullRefe renceException: Object reference not
          set to an instance of an object.

          Source Error:


          Line 104: {
          Line 105: labelMessage.Te xt =
          document.Select SingleNode("//ActivateWithTex tResponse/warning").Inner Tex
          t;
          Line 106: TextBox1.Text =
          document.Select SingleNode("//ActivatetWithTe xtResponse/result/NumberConc
          epts").InnerTex t;
          Line 107: }
          Line 108: }


          Source File: c:\inetpub\wwwr oot\helpdesk\se arch.aspx.cs Line: 106


          Not sure why line 106 is giving me problems and line 105 is not.





          Peter Bromberg [C# MVP] wrote:
          [color=blue]
          > You don't say if this is SOAP or just some sort of "plain XML"
          > response structure.
          > However, it looks like SOAP, but if you arent using a WebReference to
          > the WSDL and a WebService Proxy, it's still easy to see that both
          > responses represent a complete XmlDocument.
          >
          > So, to keep things simple, I'd load it into an XmlDocument, and look
          > for the contents of the CreateSessionRe sponse node.
          > if
          > SelectSingleNod e("//CreateSessionRe sponse/fault/faultcode").Inn erText
          >
          > is not null, you got a SOAP FAULT.
          >
          > Conversely if
          >
          > SelectSingleNod e("//CreateSessionRe sponse/return/SessionID").Inn erText
          >
          > is not null, you got a Session.
          >
          > hope that makes sense.
          >
          > Peter[/color]


          --

          Comment

          • Nick Hounsome

            #6
            Re: Help with processing xml from an http api return

            There is nothing weird about this - you just can't type

            "ActivatetWithT extResponse/result/NumberConcepts"
            ^ ^^^^^^

            Always check your paths.

            I suggest that you knock up a small app to load a doc and highlight the
            nodes selected by a path - it is quite simple and I find it extremely useful
            when the expressions get more complicated.

            "GeekCIA" <Someone@somewh ere.com> wrote in message
            news:XOednVAHx8 s3t0DeRVn-rA@comcast.com. ..[color=blue]
            > For the most part, your suggestion below is working, but now I am
            > having a weird issue with the following XML return.
            >
            > <?xml version="1.0" encoding="UTF-8" ?>
            > <ActivateWithTe xtResponse>
            > <result>0</result>
            > <warning>Activa teWithText succeeded</warning>
            > <return>
            > <NumberConcepts >33</NumberConcepts>
            > </return>
            > </ActivateWithTex tResponse>
            >
            > Here is the error segment that I am receiving...
            >
            > Exception Details: System.NullRefe renceException: Object reference not
            > set to an instance of an object.
            >
            > Source Error:
            >
            >
            > Line 104: {
            > Line 105: labelMessage.Te xt =
            > document.Select SingleNode("//ActivateWithTex tResponse/warning").Inner Tex
            > t;
            > Line 106: TextBox1.Text =
            > document.Select SingleNode("//ActivatetWithTe xtResponse/result/NumberConc
            > epts").InnerTex t;
            > Line 107: }
            > Line 108: }
            >
            >
            > Source File: c:\inetpub\wwwr oot\helpdesk\se arch.aspx.cs Line: 106
            >
            >
            > Not sure why line 106 is giving me problems and line 105 is not.
            >
            >
            >
            >
            >
            > Peter Bromberg [C# MVP] wrote:
            >[color=green]
            >> You don't say if this is SOAP or just some sort of "plain XML"
            >> response structure.
            >> However, it looks like SOAP, but if you arent using a WebReference to
            >> the WSDL and a WebService Proxy, it's still easy to see that both
            >> responses represent a complete XmlDocument.
            >>
            >> So, to keep things simple, I'd load it into an XmlDocument, and look
            >> for the contents of the CreateSessionRe sponse node.
            >> if
            >> SelectSingleNod e("//CreateSessionRe sponse/fault/faultcode").Inn erText
            >>
            >> is not null, you got a SOAP FAULT.
            >>
            >> Conversely if
            >>
            >> SelectSingleNod e("//CreateSessionRe sponse/return/SessionID").Inn erText
            >>
            >> is not null, you got a Session.
            >>
            >> hope that makes sense.
            >>
            >> Peter[/color]
            >
            >
            > --
            >[/color]


            Comment

            • Nick Hounsome

              #7
              Re: Help with processing xml from an http api return


              That didn't format correctly:

              1) you have an extra 't' at the end of Activate
              2) NumberConcepts is in the return node not the result node

              "Nick Hounsome" <nh002@nickhoun some.me.uk> wrote in message
              news:8vkDf.2171 97$D47.10783@fe 3.news.blueyond er.co.uk...[color=blue]
              > There is nothing weird about this - you just can't type
              >
              > "ActivatetWithT extResponse/result/NumberConcepts"
              > ^ ^^^^^^
              >
              > Always check your paths.
              >
              > I suggest that you knock up a small app to load a doc and highlight the
              > nodes selected by a path - it is quite simple and I find it extremely
              > useful when the expressions get more complicated.
              >
              > "GeekCIA" <Someone@somewh ere.com> wrote in message
              > news:XOednVAHx8 s3t0DeRVn-rA@comcast.com. ..[color=green]
              >> For the most part, your suggestion below is working, but now I am
              >> having a weird issue with the following XML return.
              >>
              >> <?xml version="1.0" encoding="UTF-8" ?>
              >> <ActivateWithTe xtResponse>
              >> <result>0</result>
              >> <warning>Activa teWithText succeeded</warning>
              >> <return>
              >> <NumberConcepts >33</NumberConcepts>
              >> </return>
              >> </ActivateWithTex tResponse>
              >>
              >> Here is the error segment that I am receiving...
              >>
              >> Exception Details: System.NullRefe renceException: Object reference not
              >> set to an instance of an object.
              >>
              >> Source Error:
              >>
              >>
              >> Line 104: {
              >> Line 105: labelMessage.Te xt =
              >> document.Select SingleNode("//ActivateWithTex tResponse/warning").Inner Tex
              >> t;
              >> Line 106: TextBox1.Text =
              >> document.Select SingleNode("//ActivatetWithTe xtResponse/result/NumberConc
              >> epts").InnerTex t;
              >> Line 107: }
              >> Line 108: }
              >>
              >>
              >> Source File: c:\inetpub\wwwr oot\helpdesk\se arch.aspx.cs Line: 106
              >>
              >>
              >> Not sure why line 106 is giving me problems and line 105 is not.
              >>
              >>
              >>
              >>
              >>
              >> Peter Bromberg [C# MVP] wrote:
              >>[color=darkred]
              >>> You don't say if this is SOAP or just some sort of "plain XML"
              >>> response structure.
              >>> However, it looks like SOAP, but if you arent using a WebReference to
              >>> the WSDL and a WebService Proxy, it's still easy to see that both
              >>> responses represent a complete XmlDocument.
              >>>
              >>> So, to keep things simple, I'd load it into an XmlDocument, and look
              >>> for the contents of the CreateSessionRe sponse node.
              >>> if
              >>> SelectSingleNod e("//CreateSessionRe sponse/fault/faultcode").Inn erText
              >>>
              >>> is not null, you got a SOAP FAULT.
              >>>
              >>> Conversely if
              >>>
              >>> SelectSingleNod e("//CreateSessionRe sponse/return/SessionID").Inn erText
              >>>
              >>> is not null, you got a Session.
              >>>
              >>> hope that makes sense.
              >>>
              >>> Peter[/color]
              >>
              >>
              >> --
              >>[/color]
              >
              >[/color]


              Comment

              Working...