Answer the question
In order to leave comments, you need to log in
How to store and process data in C#
Hello everyone.
The problem is this: I am writing a launcher for my project (MineCraft) in C # WPF.
Everything went well, until I realized that I still needed to store the configuration, the current profile, etc. in files.
Searched in Google, understood that it is the best of all to use - XML.
Well, I searched on this topic, I did not find a sensible one.
After all, what I need is:
1) Writing to the XML file first an empty string, like this:
<?xml version="1.0" encoding="utf-8"?>
<response>
<lastlogin>none</lastlogin>
<system>windows</system>
</response>
Answer the question
In order to leave comments, you need to log in
System.Xml.Serialization.XmlSerializer reader =
new System.Xml.Serialization.XmlSerializer(typeof(User));
System.IO.StreamReader file = new System.IO.StreamReader(
@"c:\temp\userData.xml");
UserData user= new UserData();
user = (UserData)reader.Deserialize(file);
if(user!=null)
Console.WriteLine(user.lastlogin); // тут будет доступ к полю lastlogin объекта user
public class UserData
{
public string userlogin{get;set;}
}
XmlNodeList elemList = doc.GetElementsByTagName("lastlogin");
for (int i = 0; i < elemList.Count; i++)
{
Console.WriteLine(elemList[i].InnerXml);
}
When it comes to application settings, why not use the standard .net configuration file mechanism?
In complex cases, when there are many parameters and they are divided into logical groups, you can create several configuration sections .
And in simple scenarios, if you are not afraid to get confused in the names of configuration parameters, you can get by with just the ConfigurationManager .
This is a standard dotnet mechanism, no need to worry about creating a custom file, structure, checks.
AppConfig is used to manage the application, but not to save data to it by the application. I understand that you need to save user data, and that's right, use xml. Here is an example of how to write in xml,
and here about reading. And about reading - it is better to read the entire xml file once and then only take data from the object, although it depends on the file size
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question