Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

XmlSerializer doesn't work

Discussion in 'WebGL' started by Zaborius, Oct 19, 2015.

  1. Zaborius

    Zaborius

    Joined:
    Mar 15, 2013
    Posts:
    9
    Hi there! I'm trying to make very simple xml deserialization of a very simple class, but on WebGL platform i got errors. The problem occurs only when class, that I want to deserialize is stored in precompiled plugin DLL. Maybe this is some kind of limitation of ILCPP at the moment? I couldn't find any information about it. Here is the code...

    Code (csharp):
    1.  
    2. //This class stored in precompiled dll
    3. //If you put it in the "Assets" folder, the problem disappear, but i need it this way
    4. public class Character
    5. {
    6.     public string Name {get;set;}
    7. }
    8.  
    9. //And this one lives in "Assets" folder
    10. public class Tests : MonoBehaviour
    11. {
    12.     private void Start ()
    13.     {
    14.         var rawConfig = @"<Character>
    15.                                <Name>Joe</Name>
    16.                           </Character>";
    17.  
    18.         //This line throws exception :
    19.         //InvalidOperationException: Character cannot be serialized because it
    20.             //does not have a default public constructor
    21.         //Rethrow as InvalidOperationException: There was an error reflecting
    22.             //type 'Character'.
    23.         var character = Deserialize(rawConfig);
    24.        
    25.         if (character.Name == null)
    26.         {
    27.             //This error occurs if I add [SerializableAttribute] to the "Character" class
    28.             Debug.LogError("Name is null");
    29.             return;
    30.         }
    31.  
    32.         //I can reach this line only in the editor
    33.         Debug.Log("Name : "+character.Name);
    34.     }
    35.  
    36.     public static Character Deserialize(string data)
    37.     {
    38.         using (var textReader = new StringReader(data))
    39.             return (Character)new XmlSerializer(typeof(Character)).Deserialize(textReader);
    40.     }
    41. }
    42.  
     
  2. bugglind

    bugglind

    Joined:
    Feb 12, 2016
    Posts:
    2
    Old post but I had this issue and figured out a resolution so I'm posting it here for anyone's future reference.

    You will need to add a link.xml file to your project to tell IL2CPP not to strip the types you use (it is stripping out the default constructor since it sees it is not explicitly called). You can just add your assembly reference to it and tell it to preserve everything, or be more precise with specific types, your choice. I went the former route and it resolved this problem for me. See the Unity docs on managing code stripping for full details on how to set it up.