Help Load & RW Xml

Hello
I have a problem with reading xml writing to load the highest score.
I’m working on: an Android project on Visual Studio mac

I found a piece of code that allows you to load the xml from the 3.8 monogamous Content in Copy mode by associating the DLL

string _fileName= new StreamReader(TitleContainer.OpenStream(@“Content/File.xml”)).ReadToEnd();

Code

namespace xmlTest
{
public class Score
{
// public string PlayerName { get; set; }
// public int Value { get; set; }

    public string PlayerName;
    public int Value;
}

}

<?xml version="1.0" encoding="utf-8"?>
  <PlayerName>  testPlayer </PlayerName>
  <Value> 100 </Value>

I found this code to load and write

using (var reader = new StreamReader(new FileStream(_fileName, FileMode.Open)))
{
var serilizer = new XmlSerializer(typeof(List));

          var scores = (List<Score>)serilizer.Deserialize(reader);

          return new ScoreManager(scores);

}

using (var writer = new StreamWriter(new FileStream(_fileName, FileMode.Create)))
{

            var serilizer = new XmlSerializer(typeof(List<Score>));
            serilizer.Serialize(writer, scoreManager.Scores);
  }

Error:

System.IO.DirectoryNotFoundException: Could not find a part of the path "/<?xml version="1.0" encoding="utf-8"?>

  <PlayerName>  testPlayer </PlayerName>
  <Value> 100 </Value>
". at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x00177] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/corlib/System.IO/FileStream.cs:223 at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/corlib/System.IO/FileStream.cs:81 at at (wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode) at xmlTest.Managers.ScoreManager.Load () [0x00001] in /Users/saadaneguessoum/Documents/Projets_monoGame/xmlTest/xmlTest/Managers/ScoreManager.cs:58 at xmlTest.Game1.LoadContent () [0x00028] in /Users/saadaneguessoum/Documents/Projets_monoGame/xmlTest/xmlTest/Game1.cs:57 at Microsoft.Xna.Framework.Game.Initialize () [0x00047] in :0 at xmlTest.Game1.Initialize () [0x00001] in /Users/saadaneguessoum/Documents/Projets_monoGame/xmlTest/xmlTest/Game1.cs:46 at Microsoft.Xna.Framework.Game.DoInitialize () [0x0002c] in :0 at Microsoft.Xna.Framework.AndroidGamePlatform.BeforeUpdate (Microsoft.Xna.Framework.GameTime gameTime) [0x0000e] in :0 at Microsoft.Xna.Framework.Game.DoUpdate (Microsoft.Xna.Framework.GameTime gameTime) [0x00006] in :0 at Microsoft.Xna.Framework.Game.Tick () [0x00131] in :0 at Microsoft.Xna.Framework.AndroidGameWindow.OnUpdateFrame (System.Object sender, Microsoft.Xna.Framework.MonoGameAndroidGameView+FrameEventArgs frameEventArgs) [0x00041] in :0 at Microsoft.Xna.Framework.MonoGameAndroidGameView.UpdateFrameInternal (Microsoft.Xna.Framework.MonoGameAndroidGameView+FrameEventArgs e) [0x0000f] in :0 at Microsoft.Xna.Framework.MonoGameAndroidGameView.UpdateAndRenderFrame () [0x00056] in :0 at Microsoft.Xna.Framework.MonoGameAndroidGameView.processStateRunning (System.Threading.CancellationToken token) [0x0005b] in :0 at Microsoft.Xna.Framework.MonoGameAndroidGameView.RunIteration (System.Threading.CancellationToken token) [0x000f9] in :0 at Microsoft.Xna.Framework.MonoGameAndroidGameView+<>c__DisplayClass62_0.b__0 (System.Object s) [0x00017] in :0 at Android.App.SyncContext+<>c__DisplayClass3_0.b__0 () [0x00000] in <55654ebe9f2a48e6bade2862bb243f94>:0 at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <55654ebe9f2a48e6bade2862bb243f94>:0 at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00008] in <55654ebe9f2a48e6bade2862bb243f94>:0 at at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.17(intptr,intptr)

Note: I did not find any problem with the desktop project it loads well and it saves the score

Otherwise if you have another method with Android I am a buyer

thanks in advance

Can’t really make out the error, but could it be access rights?

In my engine I can serilaize by JSON, Binary or XML, it should not matter which one you use, this looks like a IO issue.

When I get chance, Ill see if I can save and load serialized data on an Android device and let you know how I get on, will probably be tomorrow though, sorry :confused:

1 Like

Thank you so much
okay :smiley:
I am waiting for your answer
have a good day

OK, trying this out on my emulator, this is a IO access vialation as I thought, to get it to work I used
string root = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
to get the root of the application folder, I could then create a folder in there called SaveData and then read and write to a file in there.

Here is my code used in one of my engine samples:


So, I created a class to test with that holes a name and a score, populated it, serialized it, saved it to file, cleared the class out, read the serialized data back in and de serialized it back into the class instance.

Hope this helps :slight_smile:

2 Likes

Works on my S10 too :slight_smile:

1 Like

thank you so much
For your help
:smiley: I’ve been stuck on the problem for a long time

I will test it immediately
I keep you informed

1 Like

What did you use as a library I can’t find the directive
SerializationManager
Serialization
does it make any difference to work on mac?

thanks in advance

Thats my serialiser, in my engine, thats not the issue, your serialiser will be fine, its how you are accessing the file system thats the issue.

thank you for your help
As I begin I am a little lost

I think I understand my errors but I do not know how to solve them :frowning:

First the test class I don’t know how to serialize my data

Second point the SaveData folder is created but I do not know where to find it physically?

On the other hand I added the SaveData and xmlFile.xml folder seen in the project but I think I am pointing the wrong way

I am very grateful for what you have done for me

2

Ok, about to go out, ill try and be a little clearer with the directions in my next post, later.

Sorry you are struggling, we will get there in the end :slight_smile:

Okay I’m waiting for your help

thanks in advance :slight_smile:

Before I give you a long reply, I just took a look at your screen shhot, where you are creatingt he folder, you have a space in there, that should not be there. You will have created a " SaveData" folder, if that call worked, so the folder “SaveData” does not yet exist when you go to write to it…

Hello

it works thank you :smiley:

-Where are the files physically stored?
-How can I serialize the contents of the TestClass class?
thank you for spending all this time :slight_smile:

Answers to your Questions:-

  • Where ever the OS has the data section for your app, if you put a break point in, you can see the value it uses.
  • I thought you showed you had a serializer already? Use that, if it’s an XML serializer then you will need to use [Serializable] above the class definition.

Glad you got it working :slight_smile:

1 Like

Hello,

I did as you told me

by adding [Serializable]
I still have the same error :frowning:

thanks in advance

OK, you need to look up how the serializer you intend to use works, in my initial example, I used a JSON serializer, you are using an XML serialier, the XML serializer will need the class to be public, have a parameterless constructor, and the [Serailiable] attribute :slight_smile:

So, the class you want to serialize, in this case TestClass should look omething like this:
image

If you are declaring this class within another class, then that class also needs to be public.

For more information on XML serialization, check this out.

1 Like

Thank you beauoup
I created an external class TestClass

by adding the [Serializable]
and the empty constructor by default
and it works
I tested in write mode I will test in read mode to see if I have something inside I will keep you informed
thank you again :smiley:

1 Like

Hello

I tested xml reading
I have an error that I don’t understand :frowning:
am I following the algorithm well

1-create a folder + xml file on the android system
if (! Directory.Exists ($ “{root} / SaveData”))
Directory.CreateDirectory ($ “{root} / SaveData”);
2-create a TestClass class
which returns

public string Name {get; set; }
public long Score {get; set; }

3-add a function call
TestClass test = new TestClass (“test”, 0);
3-serialize the class
var xs = new XmlSerializer (typeof (TestClass));

4-write in the xml file

using (StreamWriter wr = new StreamWriter ($ “{root} /SaveData/test.xml”, false))
{
xs.Serialize (wr, test);

            wr.WriteLine (xs);
        }

but when i test reading here is the error on the image

// read data

        using (StreamReader sr = new StreamReader ($ "{root} /SaveData/test.xml"))
        {
            xs.Deserialize (sr);

            sr.ReadToEnd ();
        }

thanks in advance

It looks like you keep putting spaces in the folder for the file name…

Put it in a string variable and use that to keep it consistent.

sorry

I tested the reading it works :smiley:
I am very happy because I know that my write function is working well

i just have a question if i want to display just the player name and the score how do i do?
or a list of score and players?

thanks by avacnce :smiley:

1 Like