467,168 Members | 948 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

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

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
  • viewed: 1686
Share:
2 Replies
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

131 posts views Thread by Peter Foti | last post: by
4 posts views Thread by Ned Balzer | last post: by
4 posts views Thread by =?Utf-8?B?bW9mbGFoZXJ0eQ==?= | last post: by
13 posts views Thread by G | last post: by
reply views Thread by Nic | last post: by
1 post views Thread by Roberto | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.