473,399 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

How to safeguard losing XML file content?

Nad
Hello,

I need some help with this:

I am trying to update some data using XmlTextWriter. I instantiate first and
then do some validation and then copy data into the dataset. Finally I commit
the changes. As the following excerpt:

Now the problem is that if there is a validation failure for example,
control ends up in the catch{} block. So xmlWriter is created but nothing is
written into it. At this point it seems that the xml file becomes empty.
Because on the user interface when I exit application (without fixing my
validation error) I end up with an empty xml file. So I lose everything I had
in this file before. One solution I have is to instantiate xmlWriter just
before calling dataSet.WriteXml(xmlWriter) but still in this line an error
could occur and I will lose my data. What is the right solution to such
problem. Thanks a lot for your help.

try
{
XmlTextWriter xmlWriter = new XmlTextWriter("myFile.xml", Encoding.UTF8);
// validation ...
// writing data into dataset
dataSet.AcceptChanges();
dataSet.WriteXml(xmlWriter);
catch
{
// handle exception ...
}
finally
{
xmlWriter.Close();
}
Nov 12 '05 #1
6 1472
So you want to load the dataset up (from myfile.xml), modify its contents and
then save the changes back to myfile.xml?

"Nad" wrote:
Hello,

I need some help with this:

I am trying to update some data using XmlTextWriter. I instantiate first and
then do some validation and then copy data into the dataset. Finally I commit
the changes. As the following excerpt:

Now the problem is that if there is a validation failure for example,
control ends up in the catch{} block. So xmlWriter is created but nothing is
written into it. At this point it seems that the xml file becomes empty.
Because on the user interface when I exit application (without fixing my
validation error) I end up with an empty xml file. So I lose everything I had
in this file before. One solution I have is to instantiate xmlWriter just
before calling dataSet.WriteXml(xmlWriter) but still in this line an error
could occur and I will lose my data. What is the right solution to such
problem. Thanks a lot for your help.

try
{
XmlTextWriter xmlWriter = new XmlTextWriter("myFile.xml", Encoding.UTF8);
// validation ...
// writing data into dataset
dataSet.AcceptChanges();
dataSet.WriteXml(xmlWriter);
catch
{
// handle exception ...
}
finally
{
xmlWriter.Close();
}

Nov 12 '05 #2
Nad
Yes, and I wonder how I can prevent losing the whole content of the xml file
if something goes wrong when WriteXml fails. Because if it does in the
following code snippet I will lose the content. All I'm doing now is backing
up the file before I load it up, so at least I won't lose much.

// validation ...
// writing data into dataset
dataSet.AcceptChanges();
XmlTextWriter xmlWriter = new XmlTextWriter("myFile.xml", Encoding.UTF8);
dataSet.WriteXml(xmlWriter);
"James Mahoney" wrote:
So you want to load the dataset up (from myfile.xml), modify its contents and
then save the changes back to myfile.xml?

"Nad" wrote:
Hello,

I need some help with this:

I am trying to update some data using XmlTextWriter. I instantiate first and
then do some validation and then copy data into the dataset. Finally I commit
the changes. As the following excerpt:

Now the problem is that if there is a validation failure for example,
control ends up in the catch{} block. So xmlWriter is created but nothing is
written into it. At this point it seems that the xml file becomes empty.
Because on the user interface when I exit application (without fixing my
validation error) I end up with an empty xml file. So I lose everything I had
in this file before. One solution I have is to instantiate xmlWriter just
before calling dataSet.WriteXml(xmlWriter) but still in this line an error
could occur and I will lose my data. What is the right solution to such
problem. Thanks a lot for your help.

try
{
XmlTextWriter xmlWriter = new XmlTextWriter("myFile.xml", Encoding.UTF8);
// validation ...
// writing data into dataset
dataSet.AcceptChanges();
dataSet.WriteXml(xmlWriter);
catch
{
// handle exception ...
}
finally
{
xmlWriter.Close();
}

Nov 12 '05 #3
Write to a different file, then only if it was successful, delete the old
file and rename the new.

"Nad" <Na*@discussions.microsoft.com> wrote in message
news:70**********************************@microsof t.com...
Yes, and I wonder how I can prevent losing the whole content of the xml
file
if something goes wrong when WriteXml fails. Because if it does in the
following code snippet I will lose the content. All I'm doing now is
backing
up the file before I load it up, so at least I won't lose much.

// validation ...
// writing data into dataset
dataSet.AcceptChanges();
XmlTextWriter xmlWriter = new XmlTextWriter("myFile.xml", Encoding.UTF8);
dataSet.WriteXml(xmlWriter);
"James Mahoney" wrote:
So you want to load the dataset up (from myfile.xml), modify its contents
and
then save the changes back to myfile.xml?

"Nad" wrote:
> Hello,
>
> I need some help with this:
>
> I am trying to update some data using XmlTextWriter. I instantiate
> first and
> then do some validation and then copy data into the dataset. Finally I
> commit
> the changes. As the following excerpt:
>
> Now the problem is that if there is a validation failure for example,
> control ends up in the catch{} block. So xmlWriter is created but
> nothing is
> written into it. At this point it seems that the xml file becomes
> empty.
> Because on the user interface when I exit application (without fixing
> my
> validation error) I end up with an empty xml file. So I lose everything
> I had
> in this file before. One solution I have is to instantiate xmlWriter
> just
> before calling dataSet.WriteXml(xmlWriter) but still in this line an
> error
> could occur and I will lose my data. What is the right solution to such
> problem. Thanks a lot for your help.
>
> try
> {
> XmlTextWriter xmlWriter = new XmlTextWriter("myFile.xml",
> Encoding.UTF8);
> // validation ...
> // writing data into dataset
> dataSet.AcceptChanges();
> dataSet.WriteXml(xmlWriter);
> catch
> {
> // handle exception ...
> }
> finally
> {
> xmlWriter.Close();
> }

Nov 12 '05 #4
Nad
Thanks for the solution. I don't like xml you know, it's never as safe as
databases.

"Chris Lovett" wrote:
Write to a different file, then only if it was successful, delete the old
file and rename the new.

"Nad" <Na*@discussions.microsoft.com> wrote in message
news:70**********************************@microsof t.com...
Yes, and I wonder how I can prevent losing the whole content of the xml
file
if something goes wrong when WriteXml fails. Because if it does in the
following code snippet I will lose the content. All I'm doing now is
backing
up the file before I load it up, so at least I won't lose much.

// validation ...
// writing data into dataset
dataSet.AcceptChanges();
XmlTextWriter xmlWriter = new XmlTextWriter("myFile.xml", Encoding.UTF8);
dataSet.WriteXml(xmlWriter);
"James Mahoney" wrote:
So you want to load the dataset up (from myfile.xml), modify its contents
and
then save the changes back to myfile.xml?

"Nad" wrote:

> Hello,
>
> I need some help with this:
>
> I am trying to update some data using XmlTextWriter. I instantiate
> first and
> then do some validation and then copy data into the dataset. Finally I
> commit
> the changes. As the following excerpt:
>
> Now the problem is that if there is a validation failure for example,
> control ends up in the catch{} block. So xmlWriter is created but
> nothing is
> written into it. At this point it seems that the xml file becomes
> empty.
> Because on the user interface when I exit application (without fixing
> my
> validation error) I end up with an empty xml file. So I lose everything
> I had
> in this file before. One solution I have is to instantiate xmlWriter
> just
> before calling dataSet.WriteXml(xmlWriter) but still in this line an
> error
> could occur and I will lose my data. What is the right solution to such
> problem. Thanks a lot for your help.
>
> try
> {
> XmlTextWriter xmlWriter = new XmlTextWriter("myFile.xml",
> Encoding.UTF8);
> // validation ...
> // writing data into dataset
> dataSet.AcceptChanges();
> dataSet.WriteXml(xmlWriter);
> catch
> {
> // handle exception ...
> }
> finally
> {
> xmlWriter.Close();
> }


Nov 12 '05 #5
XML is mostly for data-exchange, not storage. So yes, if the file system
isn't cutting it, switch to SQL Server. The new SQL Express you can
download from MSDN supports XML DataType with XQuery support over columns of
that type, so you get the best of both worlds.

"Nad" <Na*@discussions.microsoft.com> wrote in message
news:7D**********************************@microsof t.com...
Thanks for the solution. I don't like xml you know, it's never as safe as
databases.

"Chris Lovett" wrote:
Write to a different file, then only if it was successful, delete the old
file and rename the new.

"Nad" <Na*@discussions.microsoft.com> wrote in message
news:70**********************************@microsof t.com...
> Yes, and I wonder how I can prevent losing the whole content of the xml
> file
> if something goes wrong when WriteXml fails. Because if it does in the
> following code snippet I will lose the content. All I'm doing now is
> backing
> up the file before I load it up, so at least I won't lose much.
>
> // validation ...
> // writing data into dataset
> dataSet.AcceptChanges();
> XmlTextWriter xmlWriter = new XmlTextWriter("myFile.xml",
> Encoding.UTF8);
> dataSet.WriteXml(xmlWriter);
>
>
> "James Mahoney" wrote:
>
>> So you want to load the dataset up (from myfile.xml), modify its
>> contents
>> and
>> then save the changes back to myfile.xml?
>>
>> "Nad" wrote:
>>
>> > Hello,
>> >
>> > I need some help with this:
>> >
>> > I am trying to update some data using XmlTextWriter. I instantiate
>> > first and
>> > then do some validation and then copy data into the dataset. Finally
>> > I
>> > commit
>> > the changes. As the following excerpt:
>> >
>> > Now the problem is that if there is a validation failure for
>> > example,
>> > control ends up in the catch{} block. So xmlWriter is created but
>> > nothing is
>> > written into it. At this point it seems that the xml file becomes
>> > empty.
>> > Because on the user interface when I exit application (without
>> > fixing
>> > my
>> > validation error) I end up with an empty xml file. So I lose
>> > everything
>> > I had
>> > in this file before. One solution I have is to instantiate xmlWriter
>> > just
>> > before calling dataSet.WriteXml(xmlWriter) but still in this line an
>> > error
>> > could occur and I will lose my data. What is the right solution to
>> > such
>> > problem. Thanks a lot for your help.
>> >
>> > try
>> > {
>> > XmlTextWriter xmlWriter = new XmlTextWriter("myFile.xml",
>> > Encoding.UTF8);
>> > // validation ...
>> > // writing data into dataset
>> > dataSet.AcceptChanges();
>> > dataSet.WriteXml(xmlWriter);
>> > catch
>> > {
>> > // handle exception ...
>> > }
>> > finally
>> > {
>> > xmlWriter.Close();
>> > }


Nov 12 '05 #6
Nad
Well I am using xml as storage, but a small and simple one.

Thanks fa lot or the SQL Express hint, I'll give it a try.

"Chris Lovett" wrote:
XML is mostly for data-exchange, not storage. So yes, if the file system
isn't cutting it, switch to SQL Server. The new SQL Express you can
download from MSDN supports XML DataType with XQuery support over columns of
that type, so you get the best of both worlds.

"Nad" <Na*@discussions.microsoft.com> wrote in message
news:7D**********************************@microsof t.com...
Thanks for the solution. I don't like xml you know, it's never as safe as
databases.

"Chris Lovett" wrote:
Write to a different file, then only if it was successful, delete the old
file and rename the new.

"Nad" <Na*@discussions.microsoft.com> wrote in message
news:70**********************************@microsof t.com...
> Yes, and I wonder how I can prevent losing the whole content of the xml
> file
> if something goes wrong when WriteXml fails. Because if it does in the
> following code snippet I will lose the content. All I'm doing now is
> backing
> up the file before I load it up, so at least I won't lose much.
>
> // validation ...
> // writing data into dataset
> dataSet.AcceptChanges();
> XmlTextWriter xmlWriter = new XmlTextWriter("myFile.xml",
> Encoding.UTF8);
> dataSet.WriteXml(xmlWriter);
>
>
> "James Mahoney" wrote:
>
>> So you want to load the dataset up (from myfile.xml), modify its
>> contents
>> and
>> then save the changes back to myfile.xml?
>>
>> "Nad" wrote:
>>
>> > Hello,
>> >
>> > I need some help with this:
>> >
>> > I am trying to update some data using XmlTextWriter. I instantiate
>> > first and
>> > then do some validation and then copy data into the dataset. Finally
>> > I
>> > commit
>> > the changes. As the following excerpt:
>> >
>> > Now the problem is that if there is a validation failure for
>> > example,
>> > control ends up in the catch{} block. So xmlWriter is created but
>> > nothing is
>> > written into it. At this point it seems that the xml file becomes
>> > empty.
>> > Because on the user interface when I exit application (without
>> > fixing
>> > my
>> > validation error) I end up with an empty xml file. So I lose
>> > everything
>> > I had
>> > in this file before. One solution I have is to instantiate xmlWriter
>> > just
>> > before calling dataSet.WriteXml(xmlWriter) but still in this line an
>> > error
>> > could occur and I will lose my data. What is the right solution to
>> > such
>> > problem. Thanks a lot for your help.
>> >
>> > try
>> > {
>> > XmlTextWriter xmlWriter = new XmlTextWriter("myFile.xml",
>> > Encoding.UTF8);
>> > // validation ...
>> > // writing data into dataset
>> > dataSet.AcceptChanges();
>> > dataSet.WriteXml(xmlWriter);
>> > catch
>> > {
>> > // handle exception ...
>> > }
>> > finally
>> > {
>> > xmlWriter.Close();
>> > }


Nov 12 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ,hj | last post by:
From: ",hj" <webmaster@mail.com> Subject: Losing random characters in HTML email Date: 28 November 2003 17:21 I've narrowed down my code as much as I can so that I can show you this bug. The...
7
by: W. Van Hooste | last post by:
Hello, I'm writing a program to display somme values as nice X graphics for a special purpose. Under here is part of my main code and part of the code to gather the sample data. the sample data...
86
by: Michael Adams | last post by:
I don't know if you have noticed, but it seems like Microsoft is losing interest in C#, and putting their energy into Visual Basic.NET instead. For instance, online chats by language since July...
4
by: Stephen | last post by:
I have a .NET (1.1 framework) application that is losing a session variable on only a few PC's. The main page is loading up in a frame in a Portal application. On the Page_Load it stores an...
5
by: fbwhite | last post by:
I know this issue has been brought up many times, but I have tried many of the solutions to no avail. I wanted to give my specific case to see if someone could be of any help. We are using the...
5
by: cFleury | last post by:
Hi, I have a public structure which is initialized only at startup time but at least one of the elements of this structure is losing its value, this particular element is ONLY initialized at the...
0
by: spacehopper_man | last post by:
hi - I am writing a "tab" control. - but it's losing the viewstate of the content pane when I switch between tabs. can anyone shed any light on why I'm losing ViewState based on my simple...
0
by: Jimmy Reds | last post by:
Hi, Sorry if this appears twice but I post through Google Groups and it had a funny 5 minutes and didn't appear to post this message the first time. I am setting session variables on a page...
1
by: JosM | last post by:
Can somebody please help me. I'm complete novice in XML. The only program i understand is DbaseIV language. The data comes in XML file. I want to change the table automatic (?java) without losing...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.