storing Hierarchical data in an xml file instead of the database...

For a project, the data needs to be sotred in a Hierarchical fashion. I've been reading articles on it and everyone seems to save it in the database. I wanted to know is there a problem with saving the data as an xml file and just updating this file rather than the database? Does anyone see any potenial problems with this?
Who is Participating?
b1xml2Connect With a Mentor Commented:
Consider the following table structure:

ID            int                  NOT NULL            IDENTITY(1,1)            Primary Key
Text            varchar 100       NOT NULL
ParentID      int                  NULL                                          Indexed (Self-Reference To ID Column)

Now that's the data stored:

Now you can grab this data into a DataSet:

Then you can transverse the hierarchy through code:

IterateNodes(table.Select("ParentID IS NULL","ID"),treeView.Nodes);

private void IterateNodes(DataRow [] rows,TreeNodeCollection collection)
      //pseudo-code using for example the WinForms Tree View
      foreach(DataRow row in rows)
            TreeNode node = new TreeNode();
            node.Text = (string)row["Text"];
            node.Tag = row["ID"].ToString() + "!" + row["ParentID"].ToString();
            IterateNodes(row.Table.Select(string.Format("ParentID = {0}",row["ID"]),"ID"),node.Nodes);

Hi dotnet22,

It Depends on the size of your data, you would not want to store many rows ()  in XML as searching and sorting would be very inefficient

There has got to be a good reason why everyone uses databases. Databases use time-tested techniques and affords efficiencies when dealing with large data. Xml is best used as a delivery mechanism between disparate, heterogeneous environments. It is useful for describing small data sets, but is not a good choice as a storage mechanism (unless it is temporary). You can use Xml to store data if you purchase Tamino Server from Software AG but it is very pricey.

I'd recommend against it if you can gain access and usage to databases. Dont re-invent the wheel.
dotnet22Author Commented:
But this is a dynamic tree structure... I'm not quite sure how to handle this in the database... Setting up the parent/child relationships
dotnet22Author Commented:
This is what I don't understand: How to store the hierrchical data in the database, then take this data and display it in a treeview and allow the user to dynamically create a new node and then setting up this relationship in the database...
All Courses

From novice to tech pro — start learning today.