tags:

views:

1295

answers:

2

Here is the story. I was doing a program, every time the program is closed all the data(File links) created by the user is lost, so whenever the user reopen the program, he does have to do all the work again.

So I decided to use an xml file to store the data, because a dbms would take too long to load and code to handle a plain text file would be hard to maintain.

The data is mainly composed of Links that stores two file paths, one for the origin and one for the destination, one boolean that represent the state of the link before the program is closed, and a few other less important things.

When the program loads the data should be read restoring the previous state of the application.

So I made a xml file like this:

...
  <Directories>
    <Directory id="0">
      <FilePath>C:\Test1</FilePath>
      <PathSeparator>\</PathSeparator>
    </Directory>
...

And used a DataSet to get the data.

Directory getDirectory()
{
    ds = new DataSet();
                ds.ReadXml(xmlPath);
    DataRow[] myRow = ds.Tables["Directory"].
                Select("id ="+id.ToString());// "id = id"
    string temp = myRow[0]["FilePath"].ToString();
    Directory result = new Directory();
    result.path = temp;
    temp = myRow[0]["PathSeparator"].ToString();
    result.pathSeparator = temp[0];
    return result;
}

All that works just fine, but then I tried to add new "rows" to the xml:

public static int addDirectory(DataSet ds, char pathSeparator, string path)
{
    DataRow myRow = ds.Tables["Directory"].NewRow();
    myRow.ItemArray[0] = 8;
    myRow.ItemArray[1] = path;
    myRow.ItemArray[2] = pathSeparator.ToString();
    myRow.ItemArray[3] = "lol";
    ds.Tables["Directory"].Rows.Add(myRow);
    ds.AcceptChanges();
    ds.WriteXml(ApplicationDataManager.xmlPath);
    return 8;
}

But all this writes is to a random place on the .xml

What am I doing wrong?

By the way if anyone have any suggestions about how I should store that data, I would be glad to hear(read).

Thanks in advance!

[Update] File Links are used to create a connection between two directories, so they can be synchronized.

+2  A: 

If LINQ is in your toolbox, I would highly recommend using that to read/write xml as opposed to a DataTable. It's much more fluent and intuitive and you can control where the nodes go... wherever you add them to the collection is where they appear in the resulting XML.

Here's a quick intro and there's a metric ton of information out there on the web about it if you search a bit: http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx

Paul Prewett
Working with DataSets is a pretty heavyweight way to go - using Linq XElement and friends will give you much more control, plus you may be able to leverage the objects you're already using internally.
Bevan
A: 

Your best option if you want a quick and dirty way is to load the users information into the dataset, call the "WriteXml" method to save and use "ReadXml" to pull it back in. The XML format that the DataSet uses on its own will come in and out in the same format.

You also have many other options, but the DataSet is the most quick and dirty way of doing it.

Mitchel Sellers