How to serialize/deserialize object to XML in PCL app?

PhenmanPhenman USMember ✭✭✭

Hello everyone! I am new in Xamarin and I have a problem for serialization object. Early I was using System.Xml.Serialization in my desktop WPF applications, but I cant't to use it in my Xamarin PCL application, because i get "SerializableAttribute could not be found" error when I try to mark class like "Serializable". Please, help me :(

Posts

  • CharwakaCharwaka INMember ✭✭✭✭✭

    Hi @Phenman

    use this

    using (var stream = new StringReader(xml))
        {
            var serializer = new XmlSerializer(typeof(Topics));
            var questionData = serializer.Deserialize (stream);
                    return questionData;
        }
    }
    

    or

    using (TextReader reader = new StreamReader("./TestData/test.xml")) {
          XmlSerializer serializer = new XmlSerializer(typeof(MyObject));
          var xml = (MyObject)serializer.Deserialize(reader);
    }
    
  • PhenmanPhenman USMember ✭✭✭
    edited December 2017

    Thank you @Charwaka for your reply! I will try it.
    But if I understand correctly this solution needs to use System.IO namespace. Therefore, I can't use PCLStorage and I must use DependencyService. Is it true?
    If it's true then I will serialize to JSON string. XML or JSON is no matter in this case.

  • CharwakaCharwaka INMember ✭✭✭✭✭

    @Phenman said:
    Thank you @Charwaka for your reply! I will try it.
    But if I understand correctly this solution needs to use System.IO namespace. Therefore, I can't use PCLStorage and I must use DependencyService. Is it true?
    If it's true then I will serialize to JSON string. XML or JSON is no matter in this case.

    yes use Dependency service

  • CharwakaCharwaka INMember ✭✭✭✭✭

    @Charwaka said:

    @Phenman said:
    Thank you @Charwaka for your reply! I will try it.
    But if I understand correctly this solution needs to use System.IO namespace. Therefore, I can't use PCLStorage and I must use DependencyService. Is it true?
    If it's true then I will serialize to JSON string. XML or JSON is no matter in this case.

    yes use Dependency service

    This might help you

    https://github.com/mono/mono/blob/master/mcs/class/corlib/System.IO/File.cs

  • PhenmanPhenman USMember ✭✭✭

    @Charwaka said:

    @Charwaka said:

    @Phenman said:
    Thank you @Charwaka for your reply! I will try it.
    But if I understand correctly this solution needs to use System.IO namespace. Therefore, I can't use PCLStorage and I must use DependencyService. Is it true?
    If it's true then I will serialize to JSON string. XML or JSON is no matter in this case.

    yes use Dependency service

    This might help you

    https://github.com/mono/mono/blob/master/mcs/class/corlib/System.IO/File.cs

    Thank you very much!

  • CharwakaCharwaka INMember ✭✭✭✭✭

    @Phenman said:

    @Charwaka said:

    @Charwaka said:

    @Phenman said:
    Thank you @Charwaka for your reply! I will try it.
    But if I understand correctly this solution needs to use System.IO namespace. Therefore, I can't use PCLStorage and I must use DependencyService. Is it true?
    If it's true then I will serialize to JSON string. XML or JSON is no matter in this case.

    yes use Dependency service

    This might help you

    https://github.com/mono/mono/blob/master/mcs/class/corlib/System.IO/File.cs

    Thank you very much!

    Please mark as answer if this works for you

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Add nuget package 'Shim' - it adds in a lot of those things such as serialize that for some silly reason are missing. Will make your transition from WPF to Xamarin a whole lot nicer.

Sign In or Register to comment.