0

I am absolutely baffled as to why this won't work. My code will write values directly to the XML file however for some odd reason I can't seem to take the values from said file and "import" them into there individual respective variables. Consider the following:

public class SaveLoadConfiguration
        {
         
            public int height { get; set; }
         
            public int width { get; set; }
           
            public int frameratecap { get; set; }
           
            public bool cap { get; set; }

public SaveLoadConfiguration LoadGameData(string fileName) // SavedGameData.xml
            {
                XmlSerializer serializer = new XmlSerializer(typeof(SaveLoadConfiguration));
                StreamReader LoadGameData = new StreamReader(fileName);
                return (SaveLoadConfiguration)serializer.Deserialize(LoadGameData);              
            }
} 

What should happen here is when I call the LoadGameData method as a property from SaveLoadConfiguration class it should simply take the XML file contents ( height, width, frameratecap , and cap variables are all written to the XML file in there respective orders ) and send them to there respective variables. However, this is simply not working. When the code compiles, regardless of what these variables in the XML file are, or what has been listed on those files on a previous state, or anything really, it prints 0 to the screen if I print anyone of those variables ( after loading them using the LoadGameData method which also should set the XML file contents to their individual respective variable correlation such as height, width, frameratecap and cap etc... ). I checked the entirety of the XML documentation, I just simply cannot fathom why it is doing it. I have suspicion that perhaps one of said XML file variables are not being setted to their respective variables within the code and thus printing it no matter the result is 0. I don't really know. If anyone can help it would be appreciated.

XML file: ( contents within it notably the height variable has already been set using a write method that writes directly to the XML file as intended )

<SaveLoadConfiguration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <height>1080</height>
  <width>0</width>
  <frameratecap>0</frameratecap>
  <cap>false</cap>
</SaveLoadConfiguration>
7
  • can you share the xml sample? Commented Jul 22, 2020 at 12:42
  • I will edit my question with it Commented Jul 22, 2020 at 12:44
  • Open xml file with notepad and see if it looks correct. Usually issue is with namespaces so you may of generated a file with namespaces and since there are no namesapces in the classes you will not get any data. Commented Jul 22, 2020 at 12:58
  • you call the LoadGameData from an existing instance? your method returns a new instance wich goes to nowhere, instead of setting the props on himself Commented Jul 22, 2020 at 13:14
  • by instance are you referring to class? Commented Jul 22, 2020 at 13:24

2 Answers 2

1

for XML-serialization you need a few more attributes:

[System.Serializable()]
[System.ComponentModel.DesignerCategory("code")]
[System.Xml.Serialization.XmlType(AnonymousType = true)]
[System.Xml.Serialization.XmlRoot(Namespace = "", IsNullable = false)]
public partial class SaveLoadConfiguration
{
  public int height { get; set; }
  public int width { get; set; }
  public int frameratecap { get; set; }
  public bool cap { get; set; }  

 public static  SaveLoadConfiguration LoadGameData(string fileName) // SavedGameData.xml
  XmlSerializer serializer = new XmlSerializer(typeof(SaveLoadConfiguration));
  using (StreamReader LoadGameData = new StreamReader(fileName))
  {
    return (SaveLoadConfiguration)serializer.Deserialize(LoadGameData);
  }
}

usage

var data = SaveLoadConfiguration.LoadGameData ("myPath");

you will be able to deserialize:

<SaveLoadConfiguration>
  <height>10</height>
  <width>10</width>
  <frameratecap>10</frameratecap>
  <cap>true</cap>
</SaveLoadConfiguration>
Sign up to request clarification or add additional context in comments.

6 Comments

so var data will contain the XML file variables ( height,` width` etc... ) because I just want the XML file variables to be correspondent to their respective counterparts within my cs file so I can access them directly.
the data holds an instance of your class with the information in the XML. You can use the instance to access the values. If you don't want a class instance, just read-write single values, then you will need a different approach: learn.microsoft.com/en-us/dotnet/csharp/programming-guide/…
How would I access the individual values within data ? Or any values for that matter?
since data is of Type SaveLoadConfiguration, you can access its properties like data.height, data.width, etc.
oh ok I see I'm going to try that! edit: Thanks, that did it!
|
0

I think you are missing

[Serializable]
public class SaveLoadConfiguration
        {

try that out

3 Comments

Unfortunately, it still prints 0 to the screen.
Could you post the xml file ?
I posted the XML file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.