I
I
Ilya2014-02-10 15:48:48
Programming
Ilya, 2014-02-10 15:48:48

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>

2) Reading from a file, it is desirable to enter immediately.
For the user to be like this:
ReadConfig("lastlogin")
and he told me what is in the lastlogin And
the same with everything else Thanks a lot in
advance .

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Mykola Dzedzinskyi, 2014-02-10
@XuPoH

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

in the program create an object class like this
public class UserData
{
     public string userlogin{get;set;}
}

If you need access like you wrote above - use something like this:
XmlNodeList elemList = doc.GetElementsByTagName("lastlogin");
    for (int i = 0; i < elemList.Count; i++)
    {
        Console.WriteLine(elemList[i].InnerXml);
    }

V
Vadim Martynov, 2014-02-10
@Vadimyan

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.

I
Ilya, 2014-02-10
@XuPoH

Excuse me, but how can I do without additional. files?

M
Mykola Dzedzinskyi, 2014-02-10
@dzedzinskiy

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 question

Ask a Question

731 491 924 answers to any question