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. Reading XML from a web service

Reading XML from a web service

Scheduled Pinned Locked Moved C#
helpdata-structuresxmltutorialquestion
7 Posts 3 Posters 0 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.
  • J Offline
    J Offline
    JD86
    wrote on last edited by
    #1

    I'm having issue understanding how I can read XML from a webservice. What webservice is sending back an object array that contains XmlAttribute and XmlElements within the array. Most of what I am finding on how to parse an XML response they are either deserializing it and converting it to their custom class or they are loading from a file. In my case I can't do neither since what is returned is an object[]. I'm not asking for code.. just some help to point me in the right direction on what process I should take for reading this information.

    P J J 3 Replies Last reply
    0
    • J JD86

      I'm having issue understanding how I can read XML from a webservice. What webservice is sending back an object array that contains XmlAttribute and XmlElements within the array. Most of what I am finding on how to parse an XML response they are either deserializing it and converting it to their custom class or they are loading from a file. In my case I can't do neither since what is returned is an object[]. I'm not asking for code.. just some help to point me in the right direction on what process I should take for reading this information.

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      If the objects within the array are well-formed XML in strings, then you can use an XmlDocument and its LoadXml method then go from there. But maybe you need to read up more on deserialization.

      J 2 Replies Last reply
      0
      • P PIEBALDconsult

        If the objects within the array are well-formed XML in strings, then you can use an XmlDocument and its LoadXml method then go from there. But maybe you need to read up more on deserialization.

        J Offline
        J Offline
        JD86
        wrote on last edited by
        #3

        The problem is objects within the array are not strings. They are of type XmlAttribute and XmlElement. So this is the return I am supposed to get:

        <response code="200" name="success">
        <message>Domain domain.com exists</message>
        </response>

        But it returns: object[] getData = soap.domainExists("domain.com"); So within getData is: getData[0] = XmlAttribute (type) getData[1] = XmlElement (type) getData[2] = XmlElement (type) getData[3] = XmlElement (type)

        1 Reply Last reply
        0
        • P PIEBALDconsult

          If the objects within the array are well-formed XML in strings, then you can use an XmlDocument and its LoadXml method then go from there. But maybe you need to read up more on deserialization.

          J Offline
          J Offline
          JD86
          wrote on last edited by
          #4

          I see. I did this:

          object[] blah = soap.domainExists("domain.com", param);

                      foreach (object o in blah)
                      {
                          Type t = o.GetType();
                          Console.WriteLine(t);
          
                          if (t == typeof(XmlElement))
                              Console.WriteLine(((XmlElement)o).InnerXml);
                          else if (t == typeof(XmlAttribute))
                              Console.WriteLine(((XmlAttribute)o).InnerXml);
                      }
          

          Which returns:

          status_code200
          System.Xml.XmlElement
          status_namesuccess
          System.Xml.XmlElement
          status_messageDomain domain.com exists

          I jsut need to figure out how to parse it

          1 Reply Last reply
          0
          • J JD86

            I'm having issue understanding how I can read XML from a webservice. What webservice is sending back an object array that contains XmlAttribute and XmlElements within the array. Most of what I am finding on how to parse an XML response they are either deserializing it and converting it to their custom class or they are loading from a file. In my case I can't do neither since what is returned is an object[]. I'm not asking for code.. just some help to point me in the right direction on what process I should take for reading this information.

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            JD86 wrote:

            I'm having issue understanding how I can read XML from a webservice.
             
            What webservice is sending back an object array that contains XmlAttribute and XmlElements within the array.

            From this and your other posts I am pretty sure your problem is that you want to 1. Get the xml 2. parse the xml 3. turn it into some sort of data structure. Steps 2 and 3 have nothng to do with a "webservice". Steps 2/3 are the same regardless of where the xml comes from. As far as a solution to 2/3 then the steps are. A. FIRST determine what is in the xml. B. Second learn how to parse xml. C. Write code using B and knowledge from A to produce what you want.

            J 1 Reply Last reply
            0
            • J jschell

              JD86 wrote:

              I'm having issue understanding how I can read XML from a webservice.
               
              What webservice is sending back an object array that contains XmlAttribute and XmlElements within the array.

              From this and your other posts I am pretty sure your problem is that you want to 1. Get the xml 2. parse the xml 3. turn it into some sort of data structure. Steps 2 and 3 have nothng to do with a "webservice". Steps 2/3 are the same regardless of where the xml comes from. As far as a solution to 2/3 then the steps are. A. FIRST determine what is in the xml. B. Second learn how to parse xml. C. Write code using B and knowledge from A to produce what you want.

              J Offline
              J Offline
              JD86
              wrote on last edited by
              #6

              Thanks. The problem I'm having is I'm expecting/wanting something in the format:

              Domain domain.com exists

              Which is what you get if you browse to the webpage. But I'm getting the format shown above which is completely different and more difficult to parse from what I can tell

              1 Reply Last reply
              0
              • J JD86

                I'm having issue understanding how I can read XML from a webservice. What webservice is sending back an object array that contains XmlAttribute and XmlElements within the array. Most of what I am finding on how to parse an XML response they are either deserializing it and converting it to their custom class or they are loading from a file. In my case I can't do neither since what is returned is an object[]. I'm not asking for code.. just some help to point me in the right direction on what process I should take for reading this information.

                J Offline
                J Offline
                JD86
                wrote on last edited by
                #7

                Ok I just parsed it my own way

                <pre lang="c#">
                object[] blah = soap.domainExists("domain.com", param);

                            bool success = false;
                            string msg = string.Empty;
                            ParseXml(blah, out success, out msg);
                

                </pre>

                <pre lang="c#">
                static void ParseXml(object[] data, out bool success, out string message)
                {
                success = false;
                message = "Failed to parse XML";

                        // Loop through objects
                        foreach (object o in data)
                        {
                            // We only care about XmlElement. Ignore the XmlAttribute
                            if (o is XmlElement)
                            {
                                // Get the innerText
                                string text = ((XmlElement)o).InnerText;
                
                                if (text.StartsWith("status\_code"))
                                {
                                    if (text.Replace("status\_code", string.Empty).Equals("200", StringComparison.CurrentCultureIgnoreCase))
                                        success = true;
                                }
                                else if (text.StartsWith("status\_message"))
                                {
                                    message = text.Replace("status\_message", string.Empty);
                                }
                            }
                        }
                    }
                

                </pre>

                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