0

Please consider the following code:

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging; 
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.IO;
using System.Xml.Linq;
using System.Runtime.Serialization.Json;
using System.Net;

namespace wideeye4
{
   public class MainViewModel : INotifyPropertyChanged
   {
    public MainViewModel()
    {
        this.Items = new ObservableCollection<ItemViewModel>();
    }

    /// <summary>
    /// A collection for ItemViewModel objects.
    /// </summary>
    public ObservableCollection<ItemViewModel> Items { get; private set; }

    private string _sampleProperty = "Sample Runtime Property Value";
    /// <summary>
    /// Sample ViewModel property; this property is used in the view to display its 
     value using a Binding
    /// </summary>
    /// <returns></returns>
    public string SampleProperty
    {
        get
        {
            return _sampleProperty;
        }
        set
        {
            if (value != _sampleProperty)
            {
                _sampleProperty = value;
                NotifyPropertyChanged("SampleProperty");
            }
        }
    }

    public bool IsDataLoaded
    {
        get;
        private set;
    }

    /// <summary>
    /// Creates and adds a few ItemViewModel objects into the Items collection.
    /// </summary>
    public void LoadData()
    {

        try
        {
            WebClient webclient = new WebClient();
            Uri uri = new Uri(<http://192.168.100.100:3000/listings.json>);
            webclient.OpenReadCompleted += new 
                    OpenReadCompletedEventHandler(webclient_openreadcompleted);
            webclient.OpenReadAsync(uri);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }       

    }

    void webclient_openreadcompleted(object sender, OpenReadCompletedEventArgs e)
    {
        DataContractJsonSerializer serializer = null;
        try
        {

            serializer = new   
       DataContractJsonSerializer(typeof(ObservableCollection<wideeye>));
            e.Result.Position = 0;
            var sr = new StreamReader(e.Result);
            var json = sr.ReadToEnd();
            ObservableCollection<wideeye> wideeyes = serializer.ReadObject(e.Result) as 
                 ObservableCollection<wideeye>;
            foreach (wideeye wd in wideeyes)
            {
                string varlisting_id = string.Empty;
                string varlisting_image_file_name = string.Empty;
                if (wd.listing != null)                
                {
                    string varcategory = wd.listing.category;
                    string varcity = wd.listing.city;
                }
                if (wd.listing_images != null)
                {
                    varlisting_id = wd.listing_images.listing_id;
                    varlisting_image_file_name =     
                   wd.listing_images.listing_image_file_name;
                }
                Items.Add(new ItemViewModel()
                { LineOne =  wd.listing.category.ToString(),  
                  LineTwo = wd.listing.city.ToString(),  
                LineThree =

    string.Format("http://wideeye.hopto.org:3000/system/listing_images/{0}/medium/{1}",
      varlisting_id, varlisting_image_file_name) });

            }
            this.IsDataLoaded = true;

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
            {
            handler(this, new PropertyChangedEventArgs(propertyName));
            }
         }
     }

  }





 {
[DataContract]
public class wideeye
 {
    [DataMember]
    public listing listing { get; set; }
    [DataMember]
    public listing_images listing_images { get; set; }
     [DataMember]
     public bids bids { get; set; }
     [DataMember]
     public messages messages { get; set; }
  }
  [DataContract]

  public class listing
  {

    public string category { get; set; }
    public string city { get; set; }
    public string country { get; set; }
    public string created_at { get; set; }
    public string current_publish_date { get; set; }
    public string details { get; set; }
    public string id { get; set; }
    public string industry { get; set; }
    public string list_exp_date { get; set; }
    public string list_price { get; set; }
    public string list_start_date { get; set; }
    public string make { get; set; }
    public string model { get; set; }
    public string open_bid { get; set; }
    public string state { get; set; }
    public string status { get; set; }
    public string title { get; set; }
    public string updated_at { get; set; }
    public string year { get; set; }

  }
  [DataContract]

  public class listing_images
   {
    public string created_at { get; set; }
    public string id { get; set; }
    public string listing_id { get; set; }
    public string listing_image_content_type { get; set; }
    public string listing_image_file_name { get; set; }
    public string listing_image_file_size { get; set; }
    public string listing_image_updated_at { get; set; }
    public string updated_at { get; set; }


   }
   [DataContract]
   public class bids
    {
    public string bid_exp_date { get; set; }
    public string bid_price { get; set; }
    public string bid_status { get; set; }
    public string created_at { get; set; }
    public string event_type { get; set; }
    public string id { get; set; }
    public string listing_id { get; set; }
    public string msg_association_id { get; set; }
    public string msg_text { get; set; }
     public string msg_type { get; set; }
    public string updated_at { get; set; }
    public string user_id { get; set; }
  }
  [DataContract]
  public class messages
  {
    public string bid_exp_date { get; set; }
    public string bid_price { get; set; }
    public string bid_status { get; set; }
    public string created_at { get; set; }
    public string event_type { get; set; }
    public string id { get; set; }
    public string listing_id { get; set; }
    public string msg_association_id { get; set; }
    public string msg_text { get; set; }
    public string msg_type { get; set; }
    public string updated_at { get; set; }
    public string user_id { get; set; }
   }
}

In webclient_openreadcompleted the event (e.result) is returning MemoryStream, so for that it is giving exception. I am facing a lot of problems as my JSON object is very large. I am using databoundWP7 model for this.

1
  • Where is the exception being thrown and what is the exception message? If your json object is large, is there any way you can reduce the size for testing? Commented Mar 28, 2012 at 16:35

1 Answer 1

2

I can't be sure - your code sample is far too large...

But I think this code reads the whole of your result into a string and then tries to read the result a second time into the serializer:

        var sr = new StreamReader(e.Result);
        var json = sr.ReadToEnd();
        ObservableCollection<wideeye> wideeyes = serializer.ReadObject(e.Result) as 
             ObservableCollection<wideeye>;

If that's true then it won't work - you can't read the stream twice (it's an incoming network stream!)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.