how to get string within string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aboon
    New Member
    • Aug 2008
    • 12

    how to get string within string

    Hi,

    I really need you guys' help with getting string within string. I have an address validation code that will send a request string to UPS, and if the address is validated, UPS will send a response string back. The problem starts here. Once I get the response string, I want to be able to display only city, state and zipcodes, but I don't know how to grab those values from the response string. The response string looks like this:
    Code:
    <?xml version="1.0"?>
    <AddressValidationResponse>
    <Response>
    <TransactionReference>
    <CustomerContext>Customer Data</CustomerContext>
    <XpciVersion>1.0001</XpciVersion></TransactionReference>
    <ResponseStatusCode>1</ResponseStatusCode>
    <ResponseStatusDescription>Success</ResponseStatusDescription></Response>
    <AddressValidationResult>
    <Rank>1</Rank>
    <Quality>0.9254</Quality>
    <Address>
    <City>MADISON</City>
    <StateProvinceCode>WI</StateProvinceCode></Address>
    <PostalCodeLowEnd>53701</PostalCodeLowEnd>
    <PostalCodeHighEnd>53708</PostalCodeHighEnd></AddressValidationResult></AddressValidationResponse>
    So, the only values I want to display are (for example)Madison , WI, 53702, 53708.

    Thanks
    aboon
  • vekipeki
    Recognized Expert New Member
    • Nov 2007
    • 229

    #2
    Check the XmlReader class: http://msdn.microsoft.com/en-us/libr...29(VS.80).aspx

    Comment

    • aboon
      New Member
      • Aug 2008
      • 12

      #3
      Originally posted by vekipeki
      Hi, Thanks for the link..I still couldn't get it to work because the XmlReader get text from a file, but the response string I got from UPS does not store in a file. I'm not sure how to just read a string within that string. I tried using SubString, but that didn't work either because the the length of the response string is not consistent. It depends on the values that user type in, and the result of the validation.

      please help,
      thanks

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        More than one way to skin an XML

        I've never used XML before (shock) and yet I can see there are 12 overrides for the .Create method - several of them use a System.IO.Strea m as the source. So you are not limited to just reading XML files from hard disc.

        I would think you can take your incoming data, turn it into a stream if it isn't already, and then feed it to the .Reader object.

        TIP:
        If you aren't accustomed to it, use the tools that Visual Studio gives you, such as the Intellisense.

        As you start typing 'XMLReader.' as soon as you hit the period a list of available methods and properties comes up. Type 'Create(', then as soon as you type the left parenthises ( a list of all the overides comes up showing you all the various ways you can call this method with different combinations of arguments. You can hit the up/down arrow keys or click on the up/down arrows of the hothelp that just popped up to see the different overrides.

        Comment

        • aboon
          New Member
          • Aug 2008
          • 12

          #5
          I'm not sure how to do it exactly..
          This is part of the code.after I got user input(city, state, zip), send the request string to UPS to validate the address.
          UPSOnlineInterf ace.Components. Address addr = new UPSOnlineInterf ace.Components. Address();
          addr.City = this.txtCity.Te xt.Trim();
          addr.StateProvi nceCode = this.ddlState.S electedValue;
          addr.PostalCode = this.txtZip.Tex t.Trim();
          string response = reques.AddressV alidateRequest( accReq, addr);
          string encodedString = HttpUtility.Htm lEncode(respons e).Replace("&lt ;", "<BR>&lt;").Rep lace("<BR>&lt;/", "&lt;/");
          The "encodingString " is the result of the response string which looks like the top post..the next step is to get the values, and dispay them which I still couldn't figure out.

          thanks

          Comment

          • vekipeki
            Recognized Expert New Member
            • Nov 2007
            • 229

            #6
            there are 12 overrides for the .Create method
            As tlhintoq said, using Intellisense can help you find the override that works for you - using the up/down keys you will browse through different tooltips for that method.

            Note the Intellisense tooltip in the figure below:


            In this case, your method accepts a TextReader, so it means you can create a StringReader from your string and pass it to your method (because StringReader is a concrete implementation of abstract TextReader class).

            Comment

            • aboon
              New Member
              • Aug 2008
              • 12

              #7
              In this case, your method accepts a TextReader, so it means you can create a StringReader from your string and pass it to your method (because StringReader is a concrete implementation of abstract TextReader class).
              Hello,
              Thanks you very much for the example. I tried using StringReader and get the innerXml as a result.
              Code:
               XmlDocument xmlDoc = new XmlDocument();
                      xmlDoc.Load(new StringReader(response));
                      XmlNode elem = xmlDoc.DocumentElement;
              The innerXml looks something like this:
              Customer Data1.00011Succ ess10.9169
              MADISONWI
              535625356220.91 69
              MADISONWI
              535935359330.91 69
              MADISONWI
              537015370840.91 69
              MADISONWI
              537115371150.91 69
              MADISONWI

              Now, I would like to add the innerXml result into a dropdownlist. Is there a way to do this???

              Thank you very much for all your help.
              aboon

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

                #8
                Code:
                ComboBox bob = new ComboBox();// Or use an existing control
                string MyNewItem = MyXMLcalculationMethod();// Your method that parses the XML and returns a string to be added to the ComboBox
                bob.Items.Add(MyNewItem);

                Comment

                • aboon
                  New Member
                  • Aug 2008
                  • 12

                  #9
                  thank you very much for all your help

                  Comment

                  Working...