Solved

How to bind xml from web service to gridview

Posted on 2006-10-30
7
510 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 500 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now