Load XML file

  • Thread starter Thread starter shapper
  • Start date Start date
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
 
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.
 
Back
Top