XmlException: Root element is missing when deserializing

I am trying to deserialize an xml file. My code is based on answers I’ve read, I googled for a solution end everyone keeps stating to check the xml to see if it has a root but I have no problem with this. This is my xml:

<?xml version="1.0" encoding="utf-8"?>
<tips>
  <tip>
All nutrition facts labels have the same format, with serving size and calories listed on top. Below this:
●	List of essential nutrients including: total fat, cholesterol, sodium, potassium, total carbohydrates and finally protein.
●	Below that: the essential nutrients, some vitamins and minerals.
●	Finally, the ingredient list.
The serving size for the product helps you determine if you actually consume the amount listed as the serving size. For example, the serving size for ready-to-eat cereal is one cup. If you eat two cups of cereal, you would need to multiply the numbers on the nutrition facts label by two.
</tip>
  
  <tip>
The total fat is important for determining what types of fat the product contains. Fats such as mono and polyunsaturated fats are more heart healthy fats while saturated and trans-fats are detrimental to overall health. The American Heart Association (AHA) recommends avoiding products that contain
any industrially manufactured trans-fat, meaning “partially hydrogenated oils”.
  </tip>
</tips>

And here is my code to find and deserialize the file:

 string absolute_path = Application.streamingAssetsPath + file_path + ".xml";
        Debug.Log("This is the file path" + absolute_path);

        WWW www = new WWW(absolute_path);

        while (!www.isDone)
        {

        }

        XmlSerializer serializer = new XmlSerializer(typeof(Tips));
        MemoryStream stream = new MemoryStream(www.bytes);

        if(stream != null)
        {
            Debug.Log("Stream is not null");
            tips = serializer.Deserialize(stream) as Tips;

        }

        stream.Close();

This is the Tips class:

[XmlRoot("tips")]
public class Tips
{
    [XmlElement("tip")]
    public List<string> tip { get; set; }
}

Any help will be greatly appreciated :slight_smile:

@Kirki_333
Try using StringReader.

    filePath = Path.Combine(Application.persistentDataPath, "filename.xml");
    if (File.Exists(loadedFile))
    {
        string text = File.ReadAllText(loadedFile);
        XmlSerializer serializer = new XmlSerializer(typeof(Tips));
        using (StringReader reader = new StringReader(text))
        {
            Tips tips = serializer.Deserialize(reader) as Tips;
        }
    }

For complete documentation of how to parse Xml, check this 1.

Well such issues are usually related to the actual data you’re putting through the XmlSerializer. Note that the XmlSerializer does not really work well with BOMs at the front of your file. While most applications just ignore it or use it to interpret the byte order of the file, the XmlSerializer will choke on it. So when editing your xml file, make sure you save it without a byte order mark. Even it should be obvious it’s generally recommended to save your file with utf8 since it is the text encoding that works best on all platforms.

If you’re on windows and even using notepad to edit your xml file, just resave it and select UTF8 (without BOM) as the encoding and you shouldn’t have any issues. Any notable text editor will allow you to omit a BOM.

If you get that file from somewhere else you may want to analyze the start of the byte stream manually and remove the BOM before feeding it to the serializer.