2

I am serializing and deserializing an Object in C# for Windows 8 Apps.

I am serializing my Object before passing it to the next View, because passing an object throws out exceptions.

function OnNavigatedTo:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
   base.OnNavigatedTo(e);
   string XMLString = e.Parameter.ToString();
   var thisChannel = XmlDeserializeFromString(XMLString, typeof(Channel));
 ....}

Deserializing Function:

  public static Channel XmlDeserializeFromString<Channel>(string objectData)
    {
        return (Channel)XmlDeserializeFromString(objectData, typeof(Channel));
    }

    public static object XmlDeserializeFromString(string objectData, Type type)
    {
        var serializer = new XmlSerializer(type);
        object result;

        using (TextReader reader = new StringReader(objectData))
        {
            result = serializer.Deserialize(reader);
        }

        return result;
    }

Object Content

I want to access the data in this Object, but something like: thisChannel.Name doesn't work. And I don't know how that I can continue working with this Object.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
MrTouch
  • 654
  • 2
  • 12
  • 28
  • 1
    var thisChannel = (Channel)XmlDeserializeFromString(XMLString, typeof(Channel)); maybe? Or use the other method available var thisChannel = XmlDeserializeFromString(XMLString); – Ric Aug 26 '13 at 13:08

4 Answers4

2

Start by dropping var in this line:

 //var thisChannel = XmlDeserializeFromString(XMLString, typeof(Channel));
 Channel thisChannel = XmlDeserializeFromString(XMLString, typeof(Channel));

and then you will at least get an error when the wrong object XmlDeserializeFromString() is selected.

And to be sure you use the right one:

 Channel thisChannel = XmlDeserializeFromString<Channel>(XMLString);

Overloading should be used with care and generally not mixed with Type parameters.

H H
  • 263,252
  • 30
  • 330
  • 514
  • well thanks to this Answer and the comment by @Ric on my Question I was able to solve the Problem. The Solution was to do it like this: Channel thisChannel = (Channel)XmlDeserializeFromString(XMLString, typeof(Channel)); – MrTouch Aug 26 '13 at 13:23
  • and now i noticed that this: Channel thisChannel = XmlDeserializeFromString(XMLString); works too ;) thanks :) – MrTouch Aug 26 '13 at 13:27
1

XmlDeserializeFromString returns an object, which does not have a Name property. You need to either:

  1. cast it to the type you want to use it as
  2. use the generic method you added which does that:

    var thisChannel = XmlDeserializeFromString<Channel>(XMLString);`
    
  3. use dynamic to resolve the method name at runtime
  4. use reflection to find the Name property at runtime
D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

Yes JSON > XML, though you want to stick to XML, use TCD.Serialization, it offers serialization and deserialization XML and JSON to/from stream & string.

.

Farhan Ghumra
  • 15,180
  • 6
  • 50
  • 115
0

Don't do this.

Passing non-primitive types through a navigation parameter will cause your application to crash when it restores from Suspend.

Only ever pass a primitive type as a navigation parameter in a Windows 8 app.

See SuspensionManager Error when application has more than 1 page in windows 8 XAML/C# app

Community
  • 1
  • 1
Jon B
  • 875
  • 7
  • 18
  • hey Jon, that's is why i am serializing my Object first. Even I still got Problems with Bitmaps, I put [XmlIgnore] before the definition of the bitmap image. – MrTouch Aug 27 '13 at 06:32
  • You should still give the video a look. http://channel9.msdn.com/Events/Build/2013/3-133 – Jon B Aug 27 '13 at 20:31