none
Consume XML webservice into a dataset RRS feed

  • Question

  • My windows forms application is consuming an xml format web service and i'm having problems deciding how to hold the data in memory once i've received it (I only read from the service and do not post back to it).

    I have been using objects to store the data but i'd like to give a dataset a try as I think using this with Linq will make things easier for me. So the question is whats the best way to 'dump' the web service data into a dataset?

    Do I need to create as strongly typed dataset to hold the information or is there a way to create the dataset 'on the fly', meaning if the format of the web service return is changed I won't need to amend my code (such as an additional property).

    I can get the data and display it in a datagridview using:
            Dim BS as New BindingSource
    
    
    BS.DataSource = webservice.getdata()
    DataGridVeiw1.datasource = BS

    This shows everthing I would expect within the datagridview. How would I get that data placed into a dataset?

    EDIT: By the way, when I say XML service, I mean it's not returning a dataset for me to just copy. VS2008 is making the items accessible via webservice.property so if there is a way to utilize this it would be a help



    • Edited by Beanze Wednesday, June 10, 2009 8:28 PM
    Wednesday, June 10, 2009 7:37 PM

Answers

  • I see what's going on. I didn't understand the output. Do you need to do anything with the output other than display it?

    You could just make a parse function that takes the object and puts it into the a DataRow. You would need to call that each time.

    Another way would be to use XmlSerializer.
    http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx

    Perhaps this can save it to Xml which you can then read into DataSet. I don't think this is really a good option though. I think you should just write one parse function to take the object and shred it into a DataRow. Sounds like you have already done this. I don't see any other way around this.

    Thanks
    Chris Robinson
    Program Manager - DataSet
    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by Beanze Friday, June 12, 2009 6:30 AM
    Thursday, June 11, 2009 11:36 PM

All replies

  • Can you attach a sample of the type data that is returned?

    I would take either of two approaches.

    1) Just try to take the results that are returned and load it into DataSet using one of the ReadXml methods
    http://msdn.microsoft.com/en-us/library/360dye2a.aspx

    This post documents in more details how to read xml data into a dataset and how it does the conversion.
    http://msdn.microsoft.com/en-us/library/fx29c3yd(VS.71).aspx

    Now if using Read Xml doesn't read in the data to the form that you want. Then I would just use and XmlReader and parse the information and put it into a DataSet directly. In this case you will want to create a DataTable with the correct DataColumns that have the types you want and then as you are reading the information you would do a table.Rows.Add(parsed data in an object array of data here)

    Thanks
    Chris Robinson
    Program Manager - DataSet
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Wednesday, June 10, 2009 11:01 PM
  • Thanks for the reply Chris.

    Lets say the webservice returns weather items, i'm able to access them like: webservice.weather.temptoday and I can allocate these using the method:

    Dim newRow As DataRow
    newRow = table.NewRow()
    newRow("Temp") = webservice.weather.temptoday
    newRow("Rain") = webservice.weather.ChanceofRain
    table.Rows.Add(newRow)
        
    Which means I don't need to use xmlreaders or anything like that (or at least I don't think I need to). What I'm looking to be able to do is create a dataset based on wesbervice.weather   - then whenever I call 'weather' it will create the columns needed (Temp & Rain) automaticly without me having to manually allocate each one. More importantly, if the weather service has a new item added like 'thunderstorms' then my dataset would automaticly accomodate and I would not need to add the new code.

    Is there a way to 'import' a schema to the dataset based on webservice.weather ?
    Thursday, June 11, 2009 6:45 AM
  • If you want a strongly typed Data Model around the request I would use the DataSet Designers. Just drag a DataTable to the designer surface. Add in all the DataColumns you need.

    Then when you get the service back, Call ReadXml to load it into this webservice. THis ReadXml will work just as I hae described before. You can test this now on an untyped DataSet to see how it works. Take the input from the webservice and read it into the DataSet using ReadXml and see what occurs.

    All of the options I dicussed above are valid regardless of whether there is an untyped interface around this or not.

    Thanks
    Chris Robinson
    Program Manager - DataSet
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Thursday, June 11, 2009 6:18 PM
  • Sorry i'm getting confused now because VS2008 stores the webservice replies/requests as objects and I cannot use them with ReadXML (it gives me an overload error).

    I don't actually know how to get at the original xml reply and not sure I want to go about trying to do that when they are already stored as objects. Maybe the title of my post was missleading but I just want to make it clear the service was not returning a dataset I could just copy in.

    What I really want is a quick easy way to dump the object containing the webservice reply into a dataset rather than having to allocate each property to it's corrct collumn. This may not sound like much but the service returns up to 30 items on some calls so it will save me lots of hassle in the end (there are about 15 different calls also).

    If using the method of newRow("Temp") = webservice.weather.temptoday is the only way then so be it. I just thought there might be an easy way to copy the object to a dataset.
    Thursday, June 11, 2009 10:18 PM
  • I see what's going on. I didn't understand the output. Do you need to do anything with the output other than display it?

    You could just make a parse function that takes the object and puts it into the a DataRow. You would need to call that each time.

    Another way would be to use XmlSerializer.
    http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx

    Perhaps this can save it to Xml which you can then read into DataSet. I don't think this is really a good option though. I think you should just write one parse function to take the object and shred it into a DataRow. Sounds like you have already done this. I don't see any other way around this.

    Thanks
    Chris Robinson
    Program Manager - DataSet
    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by Beanze Friday, June 12, 2009 6:30 AM
    Thursday, June 11, 2009 11:36 PM
  • Yeah looks like I don't have much choice. I've been playing about with trying to query the return object with Linq but its not IEnumerable so no luck there either.

    Thanks anyway.
    Friday, June 12, 2009 6:30 AM