Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to bind xml from web service to gridview

Posted on 2006-10-30
7
Medium Priority
?
526 Views
Last Modified: 2008-01-09
I am looking for an example on how to bind xml results returned from a web service to a gridview.

Regards
0
Comment
Question by:GCaron
7 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 17837094
Dim Ds As New DataSet
        Ds.ReadXml(Request.PhysicalApplicationPath + "XMLFile.xml")
        GridView1.DataSource = Ds
        GridView1.DataBind()
0
 
LVL 35

Accepted Solution

by:
YZlat earned 1500 total points
ID: 17837128
with the above example you'll have to first write your xml to a file.
If you don't want to do that, try the following:


Dim Ds As New DataSet
Ds.ReadXml(New StringReader(xmlstring))
        GridView1.DataSource = Ds
        GridView1.DataBind()


            
0
 
LVL 10

Expert Comment

by:jnhorst
ID: 17838313
Can you post a portion of what the XML looks like?  Do you have info on how the web service was developed?  If it exposes a dataset as its return type, then it might actually be very easy.  Otherwise, you will need to derive a dataset structure from the XML.  I can get you started on that, but I need to see the XML first.

John
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:deanvanrooyen
ID: 17840185
you might be better off using xslt from the returned string, less overhead.
0
 

Author Comment

by:GCaron
ID: 17849826
Thanks, I am able to at lease partially bind the xml to the gridview with limited results. The xml I return from my webservice is in the following format:

<Response>
  <title>title_name</title>
  <result>
   <resulttype>SUCCESS</resulttype>
   <resulttext>0123</resulttext>
   <records>
    <record>
     <id>1234</id>
     <name>John Doe</name>
     <address>1 Main Street</address>
    </record>
   <record>
     <id>1235</id>
     <name>Jane Doe</name>
     <address>2 Main Street</address>
    </record>
   </records>
  </result>
 </Response>

When I perform the bind my gridview only shows the title element. I am trying to display each record in the gridview.

Also, for anyone else viewing this issue, I am using the following method to call the webservice and retrieve the xml:

    public static DataSet getRecords(string strTitle)
    {
        DataSet myDS = new DataSet();

        // initialize object
        myWS.Service MyDNS = new myWS.Service();

        // get AccessCode from web.config file
        string AccessCode = ConfigurationManager.AppSettings["myWS.accesscode"].ToString();

        // make call to myWS webreference that returns and XmlNode
        XmlNode myXMLNodeObject = myWS.selectRecords(AccessCode,strTitle);

        // create new xmldocument object
        XmlDocument myXmlDocumentObject = new XmlDocument();

        // import node we received from mydns call
        myXmlDocumentObject.AppendChild(myXmlDocumentObject.ImportNode(myXMLNodeObject, true));

        //read xmldocument into xmlnodereader
        XmlNodeReader reader = new XmlNodeReader(myXmlDocumentObject);

        //read xml into dataset
        myDS.ReadXml(reader);

       //close reader
       reader.Close();

      //return dataset
        return myDS;
    }


Thanks for any additional insight you can offer.

Regards
0
 

Author Comment

by:GCaron
ID: 17849844
Additionally, on my pageload I use:

        DataSet myDS = Tools.getRecords(strTitle);
        GridView1.DataSource = myDS;
        GridView1.DataBind();

To complete the binding.
0
 

Author Comment

by:GCaron
ID: 17850876
I believe I have what I need. I have inserted the following line:


reader.ReadToFollowing("records");
myDS.ReadXml(reader);

which pushes the reader to the 'records' elements and then passes the items within the to dataset.

If anyone has something more efficient than the methods I am using it would be very much appreciated.

Regards
0

Featured Post

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question