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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. C# VS 2008 3.5 XML/Webservice error

C# VS 2008 3.5 XML/Webservice error

Scheduled Pinned Locked Moved C#
csharphelpvisual-studioxmlquestion
13 Posts 3 Posters 14 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.
  • W Wheels012

    Good morning. I am trying to populate City and State fields based on a Zip entered and I am getting an Webservice Data at the root level is invalid. Line 1, position 1. error. The XML return from the webservice is as follows:

    <?xml version="1.0" encoding="utf-8" ?>

    • <NewDataSet>
    • <Table>
      <CITY>Portland</CITY>
      <STATE>ME</STATE>
      <ZIP>04104</ZIP>
      <AREA_CODE>207</AREA_CODE>
      <TIME_ZONE>E</TIME_ZONE>
      </Table> </NewDataSet>

    My code is as follows:

    private void btnZipWS_Click(object sender, EventArgs e)
    {
    XmlDocument doc = new XmlDocument();
    Bank.net.webservicex.www.USZip ws = new Bank.net.webservicex.www.USZip(); XElement Results = XElement.Parse(ws.GetInfoByZIP (txtZip.Text).ToString());
    foreach (XElement xe in Results.Elements("NewDataSet").Elements("Table").Descendants())
    {
    if (xe.Name == "CITY")
    {
    txtCity.Text = xe.Value;
    }
    if (xe.Name == "STATE")
    { txtState.Text = xe.Value;
    }
    }
    }

    Is there an issue with the XML being returned? I wasn't sure what the - signs were for. Thank you, WHEELS

    realJSOPR Offline
    realJSOPR Offline
    realJSOP
    wrote on last edited by
    #2

    The most glaring thing is the hyphens before the root tags. Further, your code should probably look more like this:

    XElement element = Results.Elements("NewDataSet").Element("Table");
    string city = element.Element("CITY").Value;
    string state = element.Element("STATE").Value;
    ... blah blah

    .45 ACP - because shooting twice is just silly
    -----
    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
    -----
    "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

    W 1 Reply Last reply
    0
    • realJSOPR realJSOP

      The most glaring thing is the hyphens before the root tags. Further, your code should probably look more like this:

      XElement element = Results.Elements("NewDataSet").Element("Table");
      string city = element.Element("CITY").Value;
      string state = element.Element("STATE").Value;
      ... blah blah

      .45 ACP - because shooting twice is just silly
      -----
      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
      -----
      "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

      W Offline
      W Offline
      Wheels012
      wrote on last edited by
      #3

      The problem I am having now is that th eLoad is looking for a physical XML file on my computer, but it is being called from a Web Service.

      XDocument doc = XDocument.Load(ws.GetInfoByZIP(txtZip.Text).ToString());

      private void btnZipWS_Click(object sender, EventArgs e)
      {

              Bank.net.webservicex.www.USZip ws = new Bank.net.webservicex.www.USZip();
      
             
              XDocument doc = XDocument.Load(ws.GetInfoByZIP(txtZip.Text).ToString());
                       XElement element = Results.Elements("NewDataSet").Element("Table"); 
              string city = element.Element("CITY").Value; 
              string state = element.Element("STATE").Value;
      

      WHEELS

      realJSOPR 1 Reply Last reply
      0
      • W Wheels012

        The problem I am having now is that th eLoad is looking for a physical XML file on my computer, but it is being called from a Web Service.

        XDocument doc = XDocument.Load(ws.GetInfoByZIP(txtZip.Text).ToString());

        private void btnZipWS_Click(object sender, EventArgs e)
        {

                Bank.net.webservicex.www.USZip ws = new Bank.net.webservicex.www.USZip();
        
               
                XDocument doc = XDocument.Load(ws.GetInfoByZIP(txtZip.Text).ToString());
                         XElement element = Results.Elements("NewDataSet").Element("Table"); 
                string city = element.Element("CITY").Value; 
                string state = element.Element("STATE").Value;
        

        WHEELS

        realJSOPR Offline
        realJSOPR Offline
        realJSOP
        wrote on last edited by
        #4

        Since you're getting an XML string back from the web service, you want to use this:

        XDocument doc = XDocument.Parse(textZip.Text);

        .45 ACP - because shooting twice is just silly
        -----
        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
        -----
        "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

        W 1 Reply Last reply
        0
        • realJSOPR realJSOP

          Since you're getting an XML string back from the web service, you want to use this:

          XDocument doc = XDocument.Parse(textZip.Text);

          .45 ACP - because shooting twice is just silly
          -----
          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

          W Offline
          W Offline
          Wheels012
          wrote on last edited by
          #5

          Hi John. I am semi-following. I wouldn't be parsing the txtZip.Text. That is a textbox on the form and the data is being passed to the WS call.

          XDocument doc = XDocument.Parse(ws.GetInfoByZIP(txtZip.Text).ToString());

          When I use this, I am getting this error: Data at the root level is invalid. Line 1, position 1. WHEELS

          realJSOPR 1 Reply Last reply
          0
          • W Wheels012

            Hi John. I am semi-following. I wouldn't be parsing the txtZip.Text. That is a textbox on the form and the data is being passed to the WS call.

            XDocument doc = XDocument.Parse(ws.GetInfoByZIP(txtZip.Text).ToString());

            When I use this, I am getting this error: Data at the root level is invalid. Line 1, position 1. WHEELS

            realJSOPR Offline
            realJSOPR Offline
            realJSOP
            wrote on last edited by
            #6

            Like I said in my original response - you have hyphens preceding the two root-most elements. That makes your XML invalid.

            .45 ACP - because shooting twice is just silly
            -----
            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

            W 1 Reply Last reply
            0
            • realJSOPR realJSOP

              Like I said in my original response - you have hyphens preceding the two root-most elements. That makes your XML invalid.

              .45 ACP - because shooting twice is just silly
              -----
              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
              -----
              "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

              W Offline
              W Offline
              Wheels012
              wrote on last edited by
              #7

              Very new to XML. What do I do to compensate for the - s? WHEELS

              L 1 Reply Last reply
              0
              • W Wheels012

                Very new to XML. What do I do to compensate for the - s? WHEELS

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #8

                it is not valid XML as long as those hyphens are there. Eradicate them. :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                Prolific encyclopedia fixture proof-reader browser patron addict?
                We all depend on the beast below.


                W 1 Reply Last reply
                0
                • L Luc Pattyn

                  it is not valid XML as long as those hyphens are there. Eradicate them. :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                  Prolific encyclopedia fixture proof-reader browser patron addict?
                  We all depend on the beast below.


                  W Offline
                  W Offline
                  Wheels012
                  wrote on last edited by
                  #9

                  Thank you Luc.

                  realJSOPR 1 Reply Last reply
                  0
                  • W Wheels012

                    Thank you Luc.

                    realJSOPR Offline
                    realJSOPR Offline
                    realJSOP
                    wrote on last edited by
                    #10

                    Isn't that what I told you, too?

                    .45 ACP - because shooting twice is just silly
                    -----
                    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                    -----
                    "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                    W 1 Reply Last reply
                    0
                    • realJSOPR realJSOP

                      Isn't that what I told you, too?

                      .45 ACP - because shooting twice is just silly
                      -----
                      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                      -----
                      "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                      W Offline
                      W Offline
                      Wheels012
                      wrote on last edited by
                      #11

                      Hi John. I believe you did. Having a very challenging time with this XML/WebService, and didn't expect to enough these hypens. Thanks again. WHEELS

                      realJSOPR 1 Reply Last reply
                      0
                      • W Wheels012

                        Hi John. I believe you did. Having a very challenging time with this XML/WebService, and didn't expect to enough these hypens. Thanks again. WHEELS

                        realJSOPR Offline
                        realJSOPR Offline
                        realJSOP
                        wrote on last edited by
                        #12

                        I'm working on a project as we speak that's getting XML from a SQL database stored procedure via a web service, and I'm not getting any hyphens. Can I assume you solved your problem?

                        .45 ACP - because shooting twice is just silly
                        -----
                        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                        -----
                        "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                        W 1 Reply Last reply
                        0
                        • realJSOPR realJSOP

                          I'm working on a project as we speak that's getting XML from a SQL database stored procedure via a web service, and I'm not getting any hyphens. Can I assume you solved your problem?

                          .45 ACP - because shooting twice is just silly
                          -----
                          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                          -----
                          "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                          W Offline
                          W Offline
                          Wheels012
                          wrote on last edited by
                          #13

                          Found a work around, although not as elegant as I would like:

                          Bank.net.webservicex.www.USZip ws = new Bank.net.webservicex.www.USZip();

                                  XmlNode cityNode = ws.GetInfoByZIP(txtZip.Text);
                          
                                  string cityName = "UNKNOWN";
                                  string stateName = "UNKNOWN";
                                  foreach (XmlNode node in cityNode)
                                  {
                                      foreach (XmlElement elem in node)
                                      {
                                          if (elem != null && elem.Name.Equals("CITY"))
                                          {
                                              cityName = elem.FirstChild.InnerText;
                                          }
                                          if (elem != null && elem.Name.Equals("STATE"))
                                          {
                                              stateName = elem.FirstChild.InnerText;
                                          }
                                      }
                                  }
                                  txtCity.Text = cityName;
                                  txtState.Text = stateName; 
                          

                          WHEELS

                          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