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

XML being created as utf8 instead ASCII in IL2CPP

Discussion in 'Scripting' started by glennseso, Jun 18, 2015.

  1. glennseso

    glennseso

    Joined:
    Nov 11, 2011
    Posts:
    34
    Hello.

    I have a working profile saving system using XML in Mono (2.x). When I changed the build to IL2CPP to be able to upload my build to iTunes Connect the save file stopped to work.

    After some hours debugging I found that when I build the game with Mono (2.x) my xml file is encoded with ascii.

    <?xml version="1.0" encoding="us-ascii"?>

    When I tried build with IL2CPP the file was encoded with utf8.

    <?xml version="1.0" encoding="utf-8"?>



    Code (CSharp):
    1.     private void CreateProfileFile()
    2.     {
    3.  
    4.         XmlSerializer serializer = new XmlSerializer(typeof(ProfileContainer));
    5.         MemoryStream stream = new MemoryStream();
    6.  
    7.         using (stream)
    8.         {
    9.             serializer.Serialize(stream, profileContainer);
    10. //the following lines are just to test. Using Encoding.ASCII or Encoding.UTF8 in this part don't change anything even in the xmlString. When I run this in the mac I get the ASCII header and in iOS with IL2CPP I get the UTF8 header.
    11. //both strings are the same            
    12. string xmlStringAscii = Encoding.ASCII.GetString(stream.ToArray());
    13. Debug.Log (xmlStringAscii);
    14.  
    15. string xmlStringUtf8 = Encoding.UTF8.GetString(stream.ToArray());
    16. Debug.Log (xmlStringUtf8);
    17.  
    18.             EncryptDecrypt.EncryptMemory(stream, Path.Combine(Application.persistentDataPath, "profiles.xml"));
    19.         }
    20.     }
    As the encoding is defined before I save the file I need a way to force the encoding in:
    serializer.Serialize(stream, profileContainer);

    So, just to resume. This code generate a xml encoded as ASCII in mac and iOS Mono (2.x) and generate a UTF8 in iOS IL2CPP (what make the system stop to work, I already tested put a ASCII file manually in the iOS, it's works).

    Thanks
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,850
    Hi IL2CPP is not my area of expertise however you can try this:

    Code (csharp):
    1. XmlSerializer serializer = new XmlSerializer(typeof(ProfileContainer));
    2. MemoryStream stream = new MemoryStream();
    3. var streamWriter = new StreamWriter(memoryStream, System.Text.Encoding.ASCII);
    4. serializer.Serialize(streamWriter, profileContainer);
    You may also want to report this as a bug - https://unity3d.com/unity/qa/bug-reporting
     
  3. glennseso

    glennseso

    Joined:
    Nov 11, 2011
    Posts:
    34
    Hi, sorry for the delay. I can swear I already replied your message!

    This worked like a charm.

    Thanks!
     
    karl_jones likes this.