Load XML file

S

shapper

Hello,

I am opening a xml document as follows:

XDocument _categories = XDocument.Load(String.Concat(path,
"Categories.xml"), LoadOptions.SetBaseUri);

Then I am adding a few Category nodes and saving it as follows:

_mimes.Save(new Uri(_categories.BaseUri).LocalPath);

If "Categories.xml" is not found when loading how can I create a new
one with this content:

<?xml version="1.0" encoding="utf-8" ?>
<Categories/>

Then I will add the Category nodes as usual and save it at the end as
Categories.xml on the defined path.

Thanks,
Miguel
 
S

shapper

P

Peter Duniho

shapper said:
I was using File.Exists or the other option was using a Try Catch
Block. [...]

You should only use try/catch. File.Exists() is a waste of time,
because you always _have_ to have the try/catch block anyway. Even if
File.Exists() returns true, the file could disappear before you can
actually open it and load it as an XDocument, and you could experience
other errors as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top