Tech Off Thread

5 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

IsolatedStorageFileStream multiple XML root error on widows phone

Back to Forum: Tech Off
  • User profile image
    mike71

    I am new to Channel 9 so hopefully this is the correct area to write this question as I did not see any related questions when I searched the board.

    I have a class that I populate with XAML data input from a user and add the object to a List<T> and than write the list to Isolated storage.  I than read the file back into another List<T>, which seems to work fine.  The problem is when I append another List<T> to the file and read it back I recieve an error stating that I have multiple XML root elements in the file.  I am trying to make one file that contains one virtual list when read back from Isolated storage.  Since I believe that the data serializer is converting my list to an XML page is there a way to append a List<t> to the file or am I misunderstanding the usage and doing something incorrect?

    My experience with WP, C# and silverlight is from watching the Channel 9 videos and pounding the keyboard.

     

  • User profile image
    Dr Herbie

    Sounds like you're effectively appending two XML files together -- so there will be two copies of the xml header line; e.g. "<?xml version="1.0" encoding="utf-8"?>" appearing at the top of the file (as it should) but also appearing in the middle of the file (which is illegal in XML syntax).

    You need to replace the file in it's entirety each time -- concatenate the two lists into one longer list and use that as a wholesale replacement by clearing the old file and writing the new, longer list to it.

    Let us know how you get on.

    Herbie

    (and this is exactly the right place for this sort of question Smiley )

  • User profile image
    mike71

     Thanks for the help.  I am now just rewriting the file and this works fine.

    I am having another issue where I am trying to get an update passed back to my XAML page that is not working.  I have the List<T> that I am sorting and calling my NotifyPropertyChanged method but it does not seem to agree that the item has been changed so my XAML page is not updated.

    In the code below I never get passed the "if (PropertyChanged != null)" code.  It is not the best code since it calls the NotfiyPropertyChanged as it goes through the sort.

    Is a sort order change not considered a propertychange?

    Thanks again,

    Mike

     

    [DataContract]
        public class ShoppingList : INotifyPropertyChanged
        {
            [DataMember]
            public string AisleItem { get; set; }
            [DataMember]
            public string Item {get; set;}
            [DataMember]
            public string Quantity {get; set; }
            [DataMember]
            public string Units {get; set; }
            [DataMember]
            public string Store { get; set; }
            [DataMember]
            public string Aisle {get; set; }
            [DataMember]
            public Boolean PickedUp {get; set;}
    
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected void NotifyPropertyChanged(string info)
            {
                MessageBox.Show(info);
    
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(info));
                    MessageBox.Show("class.cs propertychannged");
                }
            }
    
            public int SortByAisle(ShoppingList x, ShoppingList y)
            {
    
                if (x == null)
                {
                    if (y == null)
                    { return 0; }
                    return -1;
                }
                if (y == null)
                { return 0; }
    
                NotifyPropertyChanged("AisleItem");
                return x.Aisle.CompareTo(y.Aisle);
            }
    

  • User profile image
    jugatsu

    Where's your code that changes your properties?

    If you don't subscribe to the PropertyChanged event handler it will always be null.

    public partial class Form1 : Form
    {
        ShoppingList shoppingList;
    
        public Form1()
        {
            InitializeComponent();
    
            if (this.shoppingList == null)
                this.shoppingList = ShoppingList.GetInstance();
    
            this.shoppingList.PropertyChanged += new PropertyChangedEventHandler(shoppingList_PropertyChanged);
        }
    
        void shoppingList_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            MessageBox.Show(string.Format("Property '{0}' changed.", e.PropertyName), "Property Changed Event");
        }
    
        private void btnSetValues_Click(object sender, EventArgs e)
        {
            this.shoppingList.AisleItem = this.txtAisleItem.Text;
        }
    }

    [DataContract]
    public class ShoppingList : INotifyPropertyChanged
    {
        private static ShoppingList instance = new ShoppingList();
        private string aisleItem;
    
        [DataMember]
        public string AisleItem
        {
            get { return this.aisleItem; }
            set
            {
                if (this.aisleItem != value)
                {
                    this.aisleItem = value;
                    this.OnPropertyChanged(new PropertyChangedEventArgs("AisleItem"));
                }
            }
        }
    
        [DataMember]
        public string Item { get; set; }
        [DataMember]
        public string Quantity { get; set; }
        [DataMember]
        public string Units { get; set; }
        [DataMember]
        public string Store { get; set; }
        [DataMember]
        public string Aisle { get; set; }
        [DataMember]
        public Boolean PickedUp { get; set; }
    
        private ShoppingList() { }
    
        public static ShoppingList GetInstance()
        {
            return instance;
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, e);
        }
    
        public int SortByAisle(ShoppingList x, ShoppingList y)
        {
            int compareResult = x.Aisle.CompareTo(y.Aisle);
    
            if (x == null)
            {
                compareResult = -1;
    
                if (y == null)
                    compareResult = 0;
            }
    
            if (y == null)
                compareResult = 0;
    
            return compareResult;
        }
    }

  • User profile image
    Dr Herbie

    @mike71: As Jugatsu has pointed out -- you appear to be calling PropertyChanged on AisleItem when it's value hasn't actualy been changed.  This will alert any binding targets to refresh their binding to that property, but this will have no effect as the property has not changed. And your PropertyChanged delegate will be NULL when nothing has subscribed to that event; you might want to check the call stack and see when in the process your NULL PropertyChanged handler if being fired -- it might be getting fired before the bindings are set up.

    If you're looking to sort a collection to show in the UI, you might want to consider replacing your List<> with an ObservableCollection<> and referencing this blog post to add sorting to it. Observable collection adds list-level databinding to the bindings so your UI will be updated whenever items are added to or removed from the list as well as then individial items get changed.

    Herbie

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.