469,085 Members | 1,013 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,085 developers. It's quick & easy.

Novice: Concurrent access to XML File

Hi,

Iíve created an educational software application accessed by students from a
central server. On Start-Up the application reads an XML file that stores
student progress records. This XML file stores DataSet information, one
DataTable and a number of DataRows, one per student. Once finished the
studentís DataRow is updated and the DataSet is written to the XML file.

My problem is that during the time the student has worked on the application
other students will have written to the XML file, updating their progress
information. The studentís copy of the DataSet is no longer current. Writing
to the XML file will write over data entered by other students.

Is there an effective way, at Close, to reread the XML file into the App,
update the current studentís DataRow, and then immediately write back to the
XML file. Basically I am looking for a way to lock up access to the XML file
during this process. My present Load and Closing code includes:

// On Opening

DataSet ds;

{

XmlDataDocument doc = new XmlDataDocument();

doc.DataSet.ReadXml("convowel.xml");

ds = new DataSet("TargetLetters");

ds = doc.DataSet;

}

// On Closing

private void ConVowelDialog_Closing(object sender,
System.ComponentModel.CancelEventArgs e)

{

StreamWriter myStreamWriter = new
StreamWriter(@"convowel.xml");

ds.WriteXml(myStreamWriter);

myStreamWriter.Close();

}

Any advice would be greatly appreciated.

Ted

Nov 11 '05 #1
2 7204
Ted,

an XML file is not a good data store in a multiuser scenario, but you
already found that out by yourself. The best advice I can give you is to not
use an XML file as a data store.

To work around this problem you'd face writing an abstraction layer that
hides the data store, and knows how to resolve conflicts based on an
optimistic locking strategy - e.g. you include a unique identifier and a
timestamp on every record, detect any update conflicts and reject the
update. Still, you can't work around the fact that you have to completely
lock the entire file while an update is in progress. This can severly impact
performance and scalability of your app.

A better solution is to store data in a data store that providers finer
grained data access, such as a database. There are plenty of cheap solutions
out there that work in low volume scenarios. The Microsoft way would be
MSDE, which is a free, scaled down version of SQL Server that works
seemlessly with the .NET Framework and SQLXML.

HTH,
Christoph Schittko
Software Architect, .NET Mentor
MS MVP XML .NET

"Ted Duross" <te*******@RogersNoSpam.com> wrote in message
news:e1**************@tk2msftngp13.phx.gbl...
Hi,

I've created an educational software application accessed by students from a central server. On Start-Up the application reads an XML file that stores
student progress records. This XML file stores DataSet information, one
DataTable and a number of DataRows, one per student. Once finished the
student's DataRow is updated and the DataSet is written to the XML file.

My problem is that during the time the student has worked on the application other students will have written to the XML file, updating their progress
information. The student's copy of the DataSet is no longer current. Writing to the XML file will write over data entered by other students.

Is there an effective way, at Close, to reread the XML file into the App,
update the current student's DataRow, and then immediately write back to the XML file. Basically I am looking for a way to lock up access to the XML file during this process. My present Load and Closing code includes:

// On Opening

DataSet ds;

{

XmlDataDocument doc = new XmlDataDocument();

doc.DataSet.ReadXml("convowel.xml");

ds = new DataSet("TargetLetters");

ds = doc.DataSet;

}

// On Closing

private void ConVowelDialog_Closing(object sender,
System.ComponentModel.CancelEventArgs e)

{

StreamWriter myStreamWriter = new
StreamWriter(@"convowel.xml");

ds.WriteXml(myStreamWriter);

myStreamWriter.Close();

}

Any advice would be greatly appreciated.

Ted

Nov 11 '05 #2
Dear Christoph,

Thank you very much for this information. I've started to read up on
MSDE and am quite excited by the options it provides (not to mention the
price).

Before this I had been considering another approach. My application
will likely be used by quite a small group at any one time, 20 to 30 max.
Actual writing to the XML file would be as the app closed. I was considering
making use of the FileStream's Lock method to block access to my XML file
while data was being updated. If another instance of the app attempted to
access the file when locked it would place itself into a repeat loop,
reattempting access every few seconds until successful.

Thanks again,
Ted
"Christoph" <ch*************@austin.rr.com> wrote in message
news:uy**************@TK2MSFTNGP11.phx.gbl...
Ted,

an XML file is not a good data store in a multiuser scenario, but you
already found that out by yourself. The best advice I can give you is to not use an XML file as a data store.

To work around this problem you'd face writing an abstraction layer that
hides the data store, and knows how to resolve conflicts based on an
optimistic locking strategy - e.g. you include a unique identifier and a
timestamp on every record, detect any update conflicts and reject the
update. Still, you can't work around the fact that you have to completely
lock the entire file while an update is in progress. This can severly impact performance and scalability of your app.

A better solution is to store data in a data store that providers finer
grained data access, such as a database. There are plenty of cheap solutions out there that work in low volume scenarios. The Microsoft way would be
MSDE, which is a free, scaled down version of SQL Server that works
seemlessly with the .NET Framework and SQLXML.

HTH,
Christoph Schittko
Software Architect, .NET Mentor
MS MVP XML .NET

"Ted Duross" <te*******@RogersNoSpam.com> wrote in message
news:e1**************@tk2msftngp13.phx.gbl...
Hi,

I've created an educational software application accessed by students from
a
central server. On Start-Up the application reads an XML file that

stores student progress records. This XML file stores DataSet information, one
DataTable and a number of DataRows, one per student. Once finished the
student's DataRow is updated and the DataSet is written to the XML file.

My problem is that during the time the student has worked on the

application
other students will have written to the XML file, updating their progress information. The student's copy of the DataSet is no longer current.

Writing
to the XML file will write over data entered by other students.

Is there an effective way, at Close, to reread the XML file into the App, update the current student's DataRow, and then immediately write back to

the
XML file. Basically I am looking for a way to lock up access to the XML

file
during this process. My present Load and Closing code includes:

// On Opening

DataSet ds;

{

XmlDataDocument doc = new XmlDataDocument();

doc.DataSet.ReadXml("convowel.xml");

ds = new DataSet("TargetLetters");

ds = doc.DataSet;

}

// On Closing

private void ConVowelDialog_Closing(object sender,
System.ComponentModel.CancelEventArgs e)

{

StreamWriter myStreamWriter = new
StreamWriter(@"convowel.xml");

ds.WriteXml(myStreamWriter);

myStreamWriter.Close();

}

Any advice would be greatly appreciated.

Ted


Nov 11 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by bluedolphin | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by kglaser89 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.