Stan
Stan

Reputation: 746

Serializing only some properties?

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?

Upvotes: 1

Views: 152

Answers (2)

Stan
Stan

Reputation: 746

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.

Upvotes: 0

evasilchenko
evasilchenko

Reputation: 1870

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();
}

Upvotes: 1

Related Questions