Solved

How to bind xml from web service to gridview

Posted on 2006-10-30
7
515 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

810 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