Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

XMLSerializer giving encoding errors when running build

Discussion in 'Scripting' started by Fellshadow, Apr 20, 2014.

  1. Fellshadow

    Fellshadow

    Joined:
    Dec 2, 2012
    Posts:
    169
    My serializing deserializing seems to be working fine, but when I run the build, it gives me these errors in the development console:
    Code (csharp):
    1. ArgumentException: Encoding name 'shift_jis' not supported
    2.  
    3. Parameter name: name
    4.   at System.Text.Encoding.GetEncoding (System.String name) [0x000ed] in /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Text/Encoding.cs:718
    5.  
    6.   at System.Xml.XmlInputStream.Initialize (System.IO.Stream stream) [0x00265] in /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/System.XML/System.Xml/XmlInputStream.cs:459
    7.  
    8.   at System.Xml.XmlInputStream..ctor (System.IO.Stream stream) [0x00006] in /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/System.XML/System.Xml/XmlInputStream.cs:359
    9.  
    10.   at System.Xml.XmlStreamReader..ctor (System.IO.Stream input) [0x00000] in <filename unknown>:0
    11.  
    12.   at System.Xml.XmlTextReader..ctor (System.IO.Stream input) [0x00000] in <filename unknown>:0
    13.  
    14.   at System.Xml.Serialization.XmlSerializer.Deserialize (System.IO.Stream stream) [0x00000] in /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/System.XML/System.Xml.Serialization/XmlSerializer.cs:327
    15.  
    16.   at SaveManager.Load[TitleSave] (.TitleSave obj, System.String path) [0x0005b] in D:\Projects\Novalis\Assets\Scripts\WorldManagement\SaveManager.cs:45
    17.  
    18.   at scr_TitleManager.Start () [0x00095] in D:\Projects\Novalis\Assets\Scripts\scr_TitleManager.cs:39
    As far as I'm aware, I'm not using shift_jis encoding, I'm using ASCII. My OS is Japanese, so could that be causing some sort of problem? Everything is actually working fine, but I don't really want to ignore the errors, as it could possibly cause some issues down the line.
    Anyone have any advice for this?

    Here is my serializing/deserializing code:

    Code (csharp):
    1.  
    2. using System.Xml.Serialization;
    3. using System.Xml;
    4. using System.IO;
    5. using System.Security.Cryptography;
    6. using System.Security;
    7. using System.Text;
    8.  
    9. public class SaveManager {
    10.  
    11.     // XML SAVING
    12.     public static void Save<T>(T obj, string path){
    13.         string sKey = "Testing1";
    14.  
    15.         var serializer = new XmlSerializer(obj.GetType());
    16.         var stream = new FileStream(path, FileMode.Create);
    17.  
    18.         //Create Encryption stuff
    19.         DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
    20.         DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
    21.         DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
    22.        
    23.         ICryptoTransform desencrypt = DES.CreateEncryptor();
    24.  
    25.         using(CryptoStream cStream = new CryptoStream(stream, desencrypt, CryptoStreamMode.Write)){
    26.             serializer.Serialize(cStream, obj);
    27.         }
    28.  
    29.         stream.Close ();
    30.  
    31.     }
    32.     public static void Load<T>(ref T obj, string path){
    33.         string sKey = "Testing1";
    34.  
    35.         var serializer = new XmlSerializer(obj.GetType());
    36.         var stream = new FileStream(path, FileMode.Open);
    37.  
    38.         //Create Encryption stuff
    39.         DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
    40.         DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
    41.         DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
    42.        
    43.         ICryptoTransform desdecrypt = DES.CreateDecryptor();
    44.  
    45.         using(CryptoStream cStream = new CryptoStream(stream, desdecrypt, CryptoStreamMode.Read)){
    46.             obj = (T)serializer.Deserialize(cStream);
    47.         }
    48.  
    49.         stream.Close ();
    50.     }
    51. }
    52.  
    53.  
     
  2. Fellshadow

    Fellshadow

    Joined:
    Dec 2, 2012
    Posts:
    169
    For anyone that encounters this issue in the future:
    Changing it from ASCIIEncoding.ASCII to UnicodeEncoding.ASCII fixed the issue. I assume that this is because a Japanese OS only supports shift_jis and Unicode, and when using ASCII encoding, it seems to automatically use shift_jis, which isn't supported by the XMLSerializer.