Quantcast
Viewing all articles
Browse latest Browse all 45

Error: Document element did not appear. Line 1, position 1 in Unity3d

I was trying to serialize and deserialize a class instance that saves some scene details in Unity3d. Though the instance was serializes successfully deserialization was failing with error – “Error: Document element did not appear.  Line 1, position 1.” every time. This was driving me nuts because the same serialization/deserialization code works fine in a console c# application.

The issue was related to BOM that’s included at the very beginning of the text file to know it’s encoding. This struck me because whenever I try to open c# script file from Unity, my Visual Studio always prompted to correct the encoding and line spacing of the file.

The error is annoying, but fix is relatively very simple. Do specify UTF8 encoding when you deserialize the file which will do the trick.

using (StreamReader reader = new StreamReader("filename", new UTF8Encoding(false)))

Hope this helps.

Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 45

Trending Articles