1

I'm working in Windows Phone 7, but I suppose the question may be Silverlight in general (not .NET though, as ISerializable doesn't exist in Windows Phone as suggested in this answer).

I have an number of objects I create at run time. They will mostly always be the same, except for a few properties.

So I would have as an object:

Public Class Article
    Public Property Title As String
    Public Property Author As String
    Public Property HasBeenViewed As Boolean
    Public Property DateViewed As Date
    Public Property IsPriorityArticle As Boolean
End Class

I'd like to be able to write this as:

<DataContract()>
Public Class Article
    <IgnoreDataMember()> _
    Public Property Title As String
    <IgnoreDataMember()> _
    Public Property Author As String
    <DataMember()> _
    Public Property HasBeenViewed As Boolean
    <DataMember()> _
    Public Property DateViewed As Date
    <DataMember()> _
    Public Property IsPriorityArticle As Boolean
End Class

I have a lot of article objects to load at startup. Note: Articles that are loaded at startup are the only articles - there are never any new articles or any articles deleted. The only things that are changed in the Article object is IsPriorityArticle, HasBeenViewed and DateViewed. The rest of it is static.

I load these articles into a List(Of Article) and that becomes my data source for binding. As the user views the articles, it logs when it was last viewed and that date. IsPriorityArticle may have a true or false value that can be changed by the user - the end user can mark if it is considered an important article (in which case it will show up in another page of priority articles) or to unmark it as false. If so, that new value needs to persist between sessions.

I have about 250 articles. I create these all on start up (again, there will never be more or less articles).

Initialized values sample:

Public Shared Property MyArticles As IEnumerable(Of Article)

Private Sub Application_Launching(ByVal sender As Object, ByVal e As LaunchingEventArgs)
    Dim listOfArticles As New List(Of Article)
    With listOfArticles
        .Add(New Article With {.Title = "The Internet", .Author = "John", _
                               .IsPriorityArticle = False})
        .Add(New Article With {.Title = "The World", .Author = "Jim", _
                               .IsPriorityArticle = False})
        .Add(New Article With {.Title = "The Universe", .Author = "John", _
                               .IsPriorityArticle = True})
        .Add(New Article With {.Title = "The Atom", .Author = "Jim", _
                               .IsPriorityArticle = True})
    End With
    MyArticles = listOfArticles
End Sub

(BTW, I know I don't need the line continuation character anymore, but this site won't format code without it)

Notice the last 2 - they have .IsPriorityArticle = True. That is the initialized value when the app is first launched - if the user never changes it, it will always be .IsPriorityArticle = True but if they change it to False, that should persist in IsolatedStorage to the next session.

So I've toyed around with trying to get this to serialize properly and can't get anything to work. I don't know how to load only the <DataMember> properties from IsoStore and not the rest.

Does anyone know how to do this so that my MyArticles As IEnumerable(Of Article) will use my initialized values and then check for any <DataMember> properties that need to be updated?

Or am I down the wrong path on this?

Community
  • 1
  • 1
Stan
  • 746
  • 1
  • 17
  • 35

2 Answers2

1

I hope I understood your question correctly and you can understand my reply. (It's in C# and not VB)

You can adorn the class as a datacontract:

[DataContract(Namespace = Constants.XmlNamespace)]
public class MyClass

Then just adorn each property that you want to serialize with:

[DataMember]
public string MyProperty

Then in order to serialize and write to an XML file that can sit in your store you can do:

DataContractSerializer serializer = 
                        new DataContractSerializer(typeof(MyClass));
XDocument doc = new XDocument();
using (var writer = doc.CreateWriter())
{
    serializer.WriteObject(writer, objectOfMyClass);
    writer.Close();
}
evasilchenko
  • 1,862
  • 1
  • 13
  • 26
  • Thanks. What I'm looking to do is serialize only a few properties, not the whole object. I load the variable `MyArticles` on `Application_Launching` by hydrating it with my `.Add(...` code. The point in wanting to do that is so that I don't write a bunch of stuff to IsoStore that doesn't need to be there (like `.Title`, etc.) and hopefully this speed up the serialization/deserialization times. So the question is can I do it this way? XML or DataContract are both okay to serialize - is there much of a difference? – Stan Mar 08 '12 at 23:32
  • 1
    In my opinion as long as you use the DataMember adorner only on the properties that you want to serialize and not all of them, then it's ok to serialize the entire class and save it as XML to the isoStore. I'm doing it in my application and I haven't ran into any problems yet. – evasilchenko Mar 08 '12 at 23:36
  • I'll be giving this a try soon, but quick question. So when I create a new `MyArticles` object and I don't serialize `Title` (because it never changes), will it persist in the entire class? – Stan Mar 08 '12 at 23:42
  • 1
    I'm not sure what you mean by persist in the entire class? Only the properties marked as datamember will be serialized. If you don't mark title as a datamember then it won't be serialized when you serialize the class. – evasilchenko Mar 08 '12 at 23:53
0

It appears there is no way to partially serialize a class so that it uses some kind of JOIN wherein the serialized portion is attached to the newly created portion that didn't need serializing. Serialization is of an instance of the class, not any instance of the class.

Stan
  • 746
  • 1
  • 17
  • 35