tinku92
tinku92

Reputation: 109

Regarding WPF, need advice on data sources and data binding

I am not new to WPF, but I am still a rookie. Let's say, I want to build an application which stores data about a person in an unique and separate file, and not in a database, sort of, like, Notepad. My application should do the following things.

  1. It should be able to save a person's info in an unique file.
  2. It should be able to open an user specified file and auto fill the properties/form.

How do I achieve this? Is the XML binding only way to achieve this, or is there an any other alternative? What I mean is, If I use XML binding I can write code which will enable the user to open and save different XML files, but I also read that binding to XML should be avoided from the architecture perspective. So, is there an alternative solution for my problem?

Upvotes: 0

Views: 65

Answers (3)

CharithJ
CharithJ

Reputation: 47530

Apparently your requirement is to save person details into a unique file. If you really want to use that approach, one option is using XMLSerialization.

You can create your normal person object for data binding.

When you want to save data into the specific person's file you can serialize the object and save file with a proper name (person id or so)

When you want to get Person data back from the file, you can deserialize the it directly to a person object.

//  Serialize and write to file
Person person = myPerson;
var serializer = new XmlSerializer(person.GetType());
using (var writer = XmlWriter.Create("person1.xml"))
{
    serializer.Serialize(writer, person);
}

//  Deserialize back to an instance
var serializer = new XmlSerializer(typeof(Person));
using (var reader = XmlReader.Create("person1.xml"))
{
    var person= (Person)serializer.Deserialize(reader);
}

Upvotes: 2

coder3521
coder3521

Reputation: 2646

I think if you try doing the stuff by using a Reading and writing the things to a CSV(Comma separated values) file(If not planning to implement databases) then you can achieve what you wanted.

Also if you are planning to have a separate file for each user its not at all a good idea.

Its not possible to explain everything thing here . So please have a look to link posted below , in which it has explained in detail how to achieve Reading and Writing to a csv file .

This example has been posted from here for getting full detail please look to following link Reading and writing to a csv file

Upvotes: 2

Mike Eason
Mike Eason

Reputation: 9713

For saving user data, such as sessions and settings. There are plenty of ways you can do this.

  1. Saving to data to txt files. See here.
  2. Saving data to a database. See here.
  3. My personal favourite, saving to the Settings file. See here.

These are only some of the ways you can save data locally.

Note that I mentioned saving data to a database because it is something that you shouldn't completely knock, especially if you will be saving lots of data.

To answer your question more directly, I would suggest that you go with option 3. For relatively small sets of data, like user info and user settings, it would be your best bet to save them to the built in Settings file. It's dead easy.

Good luck!

Upvotes: 1

Related Questions