none
DataService Convert From ObservableCollection to List<Type>

    Question

  • I am currently working on my first test using SL 4 and a service to return data:

    I am reading an XML file --> formatting into a class type RSSItems, however when the data is returned I get an ObservableCollection<ToadWebservice.RSSItem>.  I cannot seem to figure out how to get it into the type I need. <ToadReader.RSSItem>.

    Any ideas?  This is my first attempt with this type of project and I am unsure if I am even going down the right path.

    example:

        List<RSSItem> myFeeds = e.Result;

     

    Get an error on the conversion of e.Result to List<RSSItem>.

    Thanks,

    Just A Little Frustrated...

    Todd

    Thursday, October 21, 2010 3:21 PM

Answers

  • How are you reading your xml file?

    You can download it into the silverlight client using WebClient,

    1. Which will give you the complete xml content as string.

    2. Then convert the string into a XDocument using XDocument.Parse(xmlContentString).

    3. Perform a simpe LINQ on XDocument object and convert into whatever type you need.

    Eg.

     foreach (XElement name in NamesXml.Descendants("content"))
                        {
                            Member member = new Member();
                            member.MemberUniqueName = name.Attribute("UniqueName").Value;
                            member.MemberKey = name.Attribute("Key").Value;                                               					
                            member.MemberName = name.Attribute("Name").Value;                        
                            member.Description = name.Attribute("Description").Value;
                            myMembers.Add(member);
                        }



    Friday, October 22, 2010 3:21 AM

All replies

  • How are you reading your xml file?

    You can download it into the silverlight client using WebClient,

    1. Which will give you the complete xml content as string.

    2. Then convert the string into a XDocument using XDocument.Parse(xmlContentString).

    3. Perform a simpe LINQ on XDocument object and convert into whatever type you need.

    Eg.

     foreach (XElement name in NamesXml.Descendants("content"))
                        {
                            Member member = new Member();
                            member.MemberUniqueName = name.Attribute("UniqueName").Value;
                            member.MemberKey = name.Attribute("Key").Value;                                               					
                            member.MemberName = name.Attribute("Name").Value;                        
                            member.Description = name.Attribute("Description").Value;
                            myMembers.Add(member);
                        }



    Friday, October 22, 2010 3:21 AM
  • Thanks for the reply:

    Only question i have about this option, would be how do you define an XMLDocument within the Silverlight applications to perform the above suggestion?

    I do not have access to the XmlDocument type with in my project.  I know that I would use System.Xml but it still will not allow me to define this type within the Silverlight app.

     

    Thanks,

    T

    Tuesday, October 26, 2010 2:24 PM
  • I'm going to go on a limp here and assume you have multiple Webservices. If that is the case you will need to have your model definitions (in this case ToadReader.RSSItem) it a seperate DLL that is referenced by your project that has your ServiceClients. Otherwise your ServiceClients (when they are generated) will create their own type of RSSItem, namely ToadWebservice.RSSItem for the ToadWebservice. In other words if you had another service called FrogWebservice then you would have a FrogWebservice.RSSItem with your current set up.

    Tuesday, October 26, 2010 2:36 PM
  • Hi,

    XDocument is from System.Xml.Linq assembly.

    Thanks,

    Friday, October 29, 2010 5:50 AM
  • Thank you all for all your help.

    However, I am still not having any success!!!  System.Xml.Linq is not available in my Silverlight App (4.0), or at least I cannot make it available. Either I do not understand this enough or I am way off base.

    I originally manipulated the xml document in my service, translated that into a object class collection called RSSFeeds, defined by RSSItem.  I then could not get the object in the Silverlight application for reasons mentioned above in the first post.

    Now, I try reading the entire Xml document into the Silverlight App, only now I cannot transform it using the System.Xml.Linq assembly as described.  It appears I am way off in my understanding or missing one small piece that would make the proverbial light bulb to go on here.

    Thanks,

    T

    Monday, November 01, 2010 9:27 AM
  • However, I am still not having any success!!!  System.Xml.Linq is not available in my Silverlight App (4.0), or at least I cannot make it available. Either I do not understand this enough or I am way off base.

    What do you mean by not available. You need to right-click on project references and select 'Add Reference'. In the System tab, select System.Xml and System.Xml.Linq assemblies.

    Once you add the references, add the corresponding Using statements to the class.

    Thursday, December 09, 2010 8:39 AM