473,394 Members | 1,879 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,394 software developers and data experts.

Best Practice Advice XML

Hi,

I'm needing to update an xml file by inserting a new node. First I need to
load the xml into a XmlDocument from file.

In the first run, the file won't exist and I will have to create a new
XmlDocument. What is the best way to do this ?

I'm thinking just catching an exception and building a new doc isn't the
best way of doing this.

If i check to see if the file exists, this is not neccessarily the best way
since the file might be corrupt. I can program this, but I'm asking for
advice on the best way of doing this. Can anyone advise me?

public void savePlayer()

{

XmlDocument xmldoc = new XmlDocument();

XmlNode members;

try

{

StreamReader xmlfile = new StreamReader("members.xml");

xmldoc.Load(xmlfile);

xmlfile.Close();

//get the members node

members = xmldoc.DocumentElement;

}

catch

{

//the file doesn't exist so we must create a new document

//let's add the XML declaration section

XmlNode xmlnode=xmldoc.CreateNode(XmlNodeType.XmlDeclarati on,"","");

xmldoc.AppendChild(xmlnode);
//let's add the node element

members = xmldoc.CreateElement("members");

xmldoc.AppendChild(members);

}

XmlNode member = xmldoc.CreateElement("member");

//the display name

XmlElement displayname = xmldoc.CreateElement("displayname");

XmlText displayname_value = xmldoc.CreateTextNode("value");

displayname.AppendChild(displayname_value);

member.AppendChild(displayname); //add the username to the player
members.AppendChild(player);

xmldoc.Save("members.xml");

}
Nov 17 '05 #1
2 1824
On Mon, 23 May 2005 17:33:05 +0100, Fraser wrote:
Hi,

I'm needing to update an xml file by inserting a new node. First I need
to load the xml into a XmlDocument from file.

In the first run, the file won't exist and I will have to create a new
XmlDocument. What is the best way to do this ?

I'm thinking just catching an exception and building a new doc isn't the
best way of doing this.

And I'd suggest that your instinct is correct. The fact that a file
doesn't exist *is* an "exception"al condition; exceptions are suitable for
this task. The mindset that exception===error is just dogma.

However:

1) Ensure you only catch ans suitably code for your
"file-does-not-exist" exception, rather than every exception.

2) You'll need to cater for corrupt files whatever.

If i check to see if the file exists, this is not neccessarily the best way since the file might be corrupt. I can program this, but I'm asking
for advice on the best way of doing this. Can anyone advise me?

[sample code snipped]

Nov 17 '05 #2
On Tue, 24 May 2005 00:22:44 +0200, seani wrote:
On Mon, 23 May 2005 17:33:05 +0100, Fraser wrote:
Hi,

I'm needing to update an xml file by inserting a new node. First I need
to load the xml into a XmlDocument from file.

In the first run, the file won't exist and I will have to create a new
XmlDocument. What is the best way to do this ?

I'm thinking just catching an exception and building a new doc isn't the
best way of doing this.

And I'd suggest that your instinct is correct. The fact that a file
doesn't exist *is* an "exception"al condition; exceptions are suitable for
this task. The mindset that exception===error is just dogma.


Oops-a-daisy, I meant to say "your instinct is *incorrect*".

The rest of my post may seem more consistent now.
Nov 17 '05 #3

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

Similar topics

131
by: Peter Foti | last post by:
Simple question... which is better to use for defining font sizes and why? px and em seem to be the leading candidates. I know what the general answer is going to be, but I'm hoping to ultimately...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
6
by: AlexT | last post by:
Folks Please bear with me - I'm not a real ASP pro... I need some advice about the following problem: I have "inherited" of a working ASP site which is hosted on a collocated IIS machine. ...
13
by: Alan Silver | last post by:
Hello, MSDN (amongst other places) is full of helpful advice on ways to do data access, but they all seem geared to wards enterprise applications. Maybe I'm in a minority, but I don't have those...
4
by: Ned Balzer | last post by:
Hi all, I am pretty new to asp.net; I've done lots of classic asp, but am just beginning to get my mind wrapped around .net. What I'd like to do is include some code that tests if a user is...
4
by: =?Utf-8?B?bW9mbGFoZXJ0eQ==?= | last post by:
In VB6, we created a number of ActiveX DLLs that all shared a similar interface. The main application would load these in dynamically (late-bound.) This worked well for our situation because we...
13
by: G | last post by:
Hello, Looking for opinions on a fairly simple task, new to ASP.net (C#) and want to make sure I do this as efficiently as possible. I have a web based form, and I need to run some SQL before...
0
by: Nic | last post by:
Hi, I am looking to publish a service and is currently developing an API that I intend to allow authenticated users to use. Having been away for a while from programming, I understand that...
1
by: Roberto | last post by:
Which is the most advised way to reach SQLite from PHP5/Apache2? I have both pdo_sqlite and php5-sqlite3 installed (on Ubuntu Feisty, in case you were wondering). I want to use the best practice...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.