Loading already existing xml file doesn't work in build

I know there is already a lot of topics about that, but none of them really helped me.

I store Scores into an xml file as below

[System.Serializable]
public class oneScore
{
    public int score;
    public string name;
    public string email;

    public oneScore()
    {
        score = 0;
        name = "unnammed";
        email = "noEmail";
    }
}

[System.Serializable]
public class AllScores
{
    [XmlArray("Score")]
    public List<oneScore> list = new List<oneScore>(500);
}
<?xml version="1.0" encoding="Windows-1252"?>
<AllScores xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Score>
    <oneScore>
      <score>40</score>
      <name>sample</name>
      <email>sample@gmail.com</email>
    </oneScore>
  </Score>
</AllScores>

I’m loading Scores with :

 public void LoadScore()
    {
        XmlSerializer serializer = new XmlSerializer(typeof(AllScores));
        FileStream stream = new FileStream(Application.dataPath + "/StreamingAssets/Scores.xml", FileMode.Open);
        scores = serializer.Deserialize(stream) as AllScores;
        stream.Close();
    }

Everything’s working perfectly fine in the editor, Scores are loaded at start, new ones are well added, etc

But when I build the project on Windows, loading an already existing Scores.xml just doesn’t work.

If there is not Scores.xml, my script is able to create a new one and then save and load it fine, even if i restart the game, at the exact same path…

I really don’t understand why I can’t load an already existing xml file. I’m storing Scores.xml in StreamingAssets btw, so it is not compressed or anything.

Any ideas ?

Thx and sry for bad english

Ok so I figured out what is was on my own, and it was bullshit as always…

I’m leaving the answer here so anyone with this problem will thanks me for saving hours of stupid useless google researches :wink:

SO :

When you create an xml file in a c# script in the editor, it will look like this :

<?xml version="1.0" encoding="Windows-1252"?>
<AllScores xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Score>
    <oneScore>
      <score>120</score>
      <name>reyidiwu</name>
      <email>qiis@gmail.com</email>
    </oneScore>
  </Score>
</AllScores>

And if you create an xml file in the exact same c# script in build version, at the same path, it will look like this :

<?xml version="1.0" encoding="utf-8"?>
<AllScores xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Score>
    <oneScore>
      <score>120</score>
      <name>reyidiwu</name>
      <email>qiis@gmail.com</email>
    </oneScore>
  </Score>
</AllScores>

Now, let’s play the 7 differences game…

Oh, great, did u noticed that encodage had changed ?

So i still dont know how to encode in utf-8 from editor, but if you want to store data from editor into an xml file, and then use this xml file in build, u have to make sure it is encoded as “utf-8” and not “Windows-bullshit” manually.

EDIT : Or, u can add missing dll to support “Windows-bullshit” into your asset folder

1 Like

Nice you founded solution…

Was about to write,
Test with this also (instead of Application.dataPath)

1 Like

Thank for ur reply,

I tried ur method as many other, to get filepath, and it didn’t changed a thing, at least to fix my problem.

But I guess now my problem is “solved” I can use this instead of Application.datapath. :slight_smile:

Btw do anyone know how to encode directly from editor in utf-8 ? Doing it by hand is not a long term solution…

EDIT : I found this thread, and an another way to solve my problem is adding missing dll into your asset folder
http://answers.unity3d.com/questions/42955/codepage-1252-not-supported-works-in-editor-but-no.html

EDIT2 : This is working for Windows build, but not for Android… Any ideas again ?

Can you please post your serialization code? Normally you should use
XmlWriterSettings, XmlWriter, XmlReaderSettings, and XmlReader with the XmlSerializer because you can specify the encoding.

With android you have to read StreamingAsset files using WWW, and then the path is different also, like:

 var path = Application.streamingAssetsPath + "/myfile.txt" ;

thx jimroberts, wasn’t knowing that, I’ll try :slight_smile:

public void SaveScore()
    {
        XmlSerializer serializer = new XmlSerializer(typeof(AllScores));
        FileStream stream = new FileStream(Path.Combine(Application.streamingAssetsPath, filename), FileMode.Create);
        serializer.Serialize(stream, scores);
        stream.Close();
    }

And mgear, using WWW will work I guess, but It’s read only, I need to write in that file too…