How to read/write xml to Android app?

Having a problem reading/writing to an xml file. Error I’m getting is “Data at the root level is invalid” so assuming I’ve got something wrong with root node of XML doc? Code checks to see if a settings file exists and if not writes a copy from the default read-only version in Assets folder on creation.

string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
string filename = Path.Combine(path, "GameSettings.xml");
if (!File.Exists(filename))
{
    var xsr = new XmlSerializer(typeof(GameData));
    using (StreamReader sr = new StreamReader(Android.App.Application.Context.Assets.Open("GameSettings.xml")))
    {
        gameData = (GameData)xsr.Deserialize(sr);
     }

     // Save game settings
     var xsw = new XmlSerializer(typeof(GameData));
     using (StreamWriter sw = new StreamWriter(filename))
     {
         xsw.Serialize(sw, gameData);
         sw.WriteLine(xsw);
      }
}

I think that much runs ok but is it making a change to the root node as it writes it?

// Load game settings
string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
string filename = Path.Combine(path, "GameSettings.xml");

var xsr = new XmlSerializer(typeof(GameData));
 using (StreamReader sr = new StreamReader(filename))
 {
     gameData = (GameData)xsr.Deserialize(sr);
  }

  // XDocument xDoc = XDocument.Load(filename);

Then when a player opens the settings screen it should read the locally generated file. If I comment out the StreamReader and use XDoc.Load() it throws the same error so I think the SteamReader code is good.
Throws the error at line:

gameData = (GameData)xsr.Deserialize(sr);

Here’s the GameSettings.xml

<?xml version="1.0" encoding="utf-8" ?>
<GameData xmlns="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<strDifficulty>Medium</strDifficulty>
<strSound>On</strSound>
</GameData>

The GameData class.

[Serializable]
public class GameData
{
    public string strDifficulty { get; set; }
    public string strSound { get; set; }

    public GameData()
    {
    }

    public GameData(string difficulty, string sound)
    {
        strDifficulty = difficulty;
        strSound = sound;
    }
}

Any suggestions how I’m nerfing the root node?

Thanks.

This is more opinion than technical expertise:

Getting XML to work for me was a chore at first, even on windows… With no prior familiarity, it’s difficult to trouble-shoot the first few times…

I would recommend setting up a real easy example, follw a tut perhaps, something standard like a fake customer catalog, that’s what I did… That will make it easier to get help, if nothing else… And once you can do that, saving your game data should be just more of the same.

Hello. Unfortunately for what I’m trying to do that’s about as simple as it gets I think.

I would use json instead of xml. But that is maybe only personal preference. Make sure you don’t try to parse the xml yourself. I tried this the first time I used XML and did not know about complete solutions (automatic serialization / de serialization) which do the “work” for you.

I’ve heard that a few times now, I’ll keep it in mind for next time.
Be nice to have the code for the xml finished though. I think the serialisation/deserialisation works ok apart from the root node of the xml.

It’s not the BOM, tried that already.

I removed the writeline from the Serialize code and it works now.

var xsw = new XmlSerializer(typeof(GameData));
 using (StreamWriter sw = new StreamWriter(filename))
 {
     xsw.Serialize(sw, gameData);
  }

I have my save-code set up to WriteLine everything in my game, using for-loops on object-lists, storing every desired object instance including variables. It’s not that complicated but it did take some time… What is this “serialization” you speak of?

To quote the Microsoft docs:

“XML serialization is the process of converting an object’s public properties and fields to a serial format (in this case, XML)”.

XmlSerializer Class (System.Xml.Serialization) | Microsoft Docs

Basically just saves the object as xml and then deserialisation reads from an xml file and creates an object, the GameData class in my code.

Then there’s the JSON serialiser, same thing but converts it to JSON instead of XML.

JsonSerializer Class (System.Text.Json) | Microsoft Docs