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. Best way to read string xml? [Solved]

Best way to read string xml? [Solved]

Scheduled Pinned Locked Moved C#
xmlquestion
10 Posts 5 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.
  • B Offline
    B Offline
    Boipelo
    wrote on last edited by
    #1

    Fisrt let me say I dont have errors and all is working fine, I want to know the best way of doing this... I am consumming a web serving that return string xml...

    33True

    I want the value "33", have the following and it works fine but wanted to know it's the right way to go...

    string sReturnString = WebserviceName.ReturnMyValue(PassingSomeValues);
    foreach (char cString in sReturnString.ToCharArray())
    {
    if (char.IsDigit(cString))
    sCredits += cString.ToString();
    }
    return "Balance: sCredits";

    I don't want to save it to a file, but just want to dislapy the value. Any recommnedations? Thanking you in advance. Addition: .. Saving it to an xml file makes it easy to read the creadit element value 33; but I dont want to create any files..

    I remain joe!

    Richard DeemingR P J 3 Replies Last reply
    0
    • B Boipelo

      Fisrt let me say I dont have errors and all is working fine, I want to know the best way of doing this... I am consumming a web serving that return string xml...

      33True

      I want the value "33", have the following and it works fine but wanted to know it's the right way to go...

      string sReturnString = WebserviceName.ReturnMyValue(PassingSomeValues);
      foreach (char cString in sReturnString.ToCharArray())
      {
      if (char.IsDigit(cString))
      sCredits += cString.ToString();
      }
      return "Balance: sCredits";

      I don't want to save it to a file, but just want to dislapy the value. Any recommnedations? Thanking you in advance. Addition: .. Saving it to an xml file makes it easy to read the creadit element value 33; but I dont want to create any files..

      I remain joe!

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2
      1. If you don't already have one, add a reference to System.Xml.Linq;

      2. Add using System.Linq; and using System.Xml.Linq; at the top of your code file;

      3. Use:

        XElement el = XElement.Parse(sReturnString);
        string sCredits = (string)el.Elements("data").Elements("credits").FirstOrDefault();
        return "Balance: " + sCredits;


      "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. If you don't already have one, add a reference to System.Xml.Linq;

        2. Add using System.Linq; and using System.Xml.Linq; at the top of your code file;

        3. Use:

          XElement el = XElement.Parse(sReturnString);
          string sCredits = (string)el.Elements("data").Elements("credits").FirstOrDefault();
          return "Balance: " + sCredits;


        "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
        Boipelo
        wrote on last edited by
        #3

        Thanks, appreciated!

        I remain joe!

        K 1 Reply Last reply
        0
        • B Boipelo

          Fisrt let me say I dont have errors and all is working fine, I want to know the best way of doing this... I am consumming a web serving that return string xml...

          33True

          I want the value "33", have the following and it works fine but wanted to know it's the right way to go...

          string sReturnString = WebserviceName.ReturnMyValue(PassingSomeValues);
          foreach (char cString in sReturnString.ToCharArray())
          {
          if (char.IsDigit(cString))
          sCredits += cString.ToString();
          }
          return "Balance: sCredits";

          I don't want to save it to a file, but just want to dislapy the value. Any recommnedations? Thanking you in advance. Addition: .. Saving it to an xml file makes it easy to read the creadit element value 33; but I dont want to create any files..

          I remain joe!

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

          I'd use an XmlDocument.

          string s = "<api_result><data><credits>33</credits></data><call_result><result>True</result><error /></call_result></api_result>" ;

          System.Xml.XmlDocument doc = new System.Xml.XmlDocument() ;
          doc.LoadXml ( s ) ;

          System.Xml.XmlNode nod = doc.DocumentElement.SelectSingleNode ( "data/credits" ) ;

          string v = nod.InnerText ;

          B 1 Reply Last reply
          0
          • B Boipelo

            Fisrt let me say I dont have errors and all is working fine, I want to know the best way of doing this... I am consumming a web serving that return string xml...

            33True

            I want the value "33", have the following and it works fine but wanted to know it's the right way to go...

            string sReturnString = WebserviceName.ReturnMyValue(PassingSomeValues);
            foreach (char cString in sReturnString.ToCharArray())
            {
            if (char.IsDigit(cString))
            sCredits += cString.ToString();
            }
            return "Balance: sCredits";

            I don't want to save it to a file, but just want to dislapy the value. Any recommnedations? Thanking you in advance. Addition: .. Saving it to an xml file makes it easy to read the creadit element value 33; but I dont want to create any files..

            I remain joe!

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

            Boipelo wrote:

            I want to know the best way of doing this...

            Depends on what "best" means. But since you are doing nothing but a simple value lookup what you are doing is probably the most performant (although probably not in a significant way.) There are upside/downsides to ignoring the XML itself. For example if they change element names on the message your code still works. But if they add another element first with a numeric value it doesn't. Conversely using XML if they change names then the code would fail. But if they add another element it would still work.

            B 1 Reply Last reply
            0
            • J jschell

              Boipelo wrote:

              I want to know the best way of doing this...

              Depends on what "best" means. But since you are doing nothing but a simple value lookup what you are doing is probably the most performant (although probably not in a significant way.) There are upside/downsides to ignoring the XML itself. For example if they change element names on the message your code still works. But if they add another element first with a numeric value it doesn't. Conversely using XML if they change names then the code would fail. But if they add another element it would still work.

              B Offline
              B Offline
              Boipelo
              wrote on last edited by
              #6

              I was also scared of "...if they add another element with a numeric value". I am not seeing them changing the "element name", that wont happen. Thanks for the comment.

              I remain joe!

              J 1 Reply Last reply
              0
              • P PIEBALDconsult

                I'd use an XmlDocument.

                string s = "<api_result><data><credits>33</credits></data><call_result><result>True</result><error /></call_result></api_result>" ;

                System.Xml.XmlDocument doc = new System.Xml.XmlDocument() ;
                doc.LoadXml ( s ) ;

                System.Xml.XmlNode nod = doc.DocumentElement.SelectSingleNode ( "data/credits" ) ;

                string v = nod.InnerText ;

                B Offline
                B Offline
                Boipelo
                wrote on last edited by
                #7

                Thanks...

                I remain joe!

                1 Reply Last reply
                0
                • B Boipelo

                  Thanks, appreciated!

                  I remain joe!

                  K Offline
                  K Offline
                  Keld Olykke
                  wrote on last edited by
                  #8

                  In my opinion this (or using XmlReader directly) is the way to go: Consistency - the web service uses xml responses because it is a standard with some pros e.g. extensibility and formatting. By not parsing xml according to the standard, you don't get the benefits from the standard. Performance - XmlElement.parse is built upon XmlReader which is a sequential reader. It is both faster and cheaper in memory than XmlDocument that builds a complete DOM in memory. Kind Regards, Keld Ølykke

                  B 1 Reply Last reply
                  0
                  • K Keld Olykke

                    In my opinion this (or using XmlReader directly) is the way to go: Consistency - the web service uses xml responses because it is a standard with some pros e.g. extensibility and formatting. By not parsing xml according to the standard, you don't get the benefits from the standard. Performance - XmlElement.parse is built upon XmlReader which is a sequential reader. It is both faster and cheaper in memory than XmlDocument that builds a complete DOM in memory. Kind Regards, Keld Ølykke

                    B Offline
                    B Offline
                    Boipelo
                    wrote on last edited by
                    #9

                    @Keld, thanks for the info...

                    I remain joe!

                    1 Reply Last reply
                    0
                    • B Boipelo

                      I was also scared of "...if they add another element with a numeric value". I am not seeing them changing the "element name", that wont happen. Thanks for the comment.

                      I remain joe!

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

                      Boipelo wrote:

                      I was also scared of "...if they add another element with a numeric value"

                      but only if it is first. You can do a indexOf for the element name and then start parsing the numeric.

                      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