Load XML file

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
0
shapper
11/16/2009 5:59:20 PM
dotnet.languages.csharp 1931 articles. 0 followers. Follow

3 Replies
1079 Views

Similar Articles

[PageSpeed] 14

shapper wrote:
> [...]
> 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/>

http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.xdocument.aspx
0
Peter
11/16/2009 6:59:14 PM
On Nov 16, 6:59=A0pm, Peter Duniho <no.peted.s...@no.nwlink.spam.com>
wrote:
> shapper wrote:
> > [...]
> > If "Categories.xml" is not found when loading how can I create a new
> > one with this content:
>
> > <?xml version=3D"1.0" encoding=3D"utf-8" ?>
> > <Categories/>
>
> http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.xdo...

Hi Peter,

I was using File.Exists or the other option was using a Try Catch
Block.
If it does not exist than I would create the XDocument.

Anyway, I was able to solve it this way.

Thanks,
Miguel
0
shapper
11/16/2009 9:42:16 PM
shapper wrote:
> 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.
0
Peter
11/16/2009 10:11:20 PM
Reply:

Similar Artilces:

.cab file import into Outlook
I want to get rid of Incredimail XE and import all the email data back into Outlook where it originally came from. Incredimail exports it in a .cab file format that I saved on my desktop. I do not see that type of file as an option for importing into Outlook. Does anyone know how I would do this? ...

reading data from .xls file
Hi, My boss "ask" me to prepare so called application, which will be able to create report about certain data. Some people are working on their files /.xls/ which consist of lots of data, but I only need to get from those files only certain data /e.g. name of customer, date, number of order, quantity/. I don't want to open their files /using vba/ to copy data because it's too long /and I don't know when my cooworkers are using their files/. The question is - is there any method to read /retrieve/ data from that files without openning them on my computer ...

Does Publisher 2003 import pdf files
I want to import a pdf file as a graphic into Publisher. Does 2003 do this? I have 2002 and it does not! Kathy <Kathy@discussions.microsoft.com> was very recently heard to utter: > I want to import a pdf file as a graphic into Publisher. Does 2003 > do this? I have 2002 and it does not! It does not. See here: http://ed.mvps.org/Static.aspx?=Publisher/convertpdf -- Ed Bennett - MVP Microsoft Publisher http://ed.mvps.org/ The only way I've been able to do this is to use the 'Snapshot Tool' in Acrobat (don't think this is available with Reader) and paste ...

Excel quits responding searching for files not in my documents
Office 2003 When I use CTRL O to open a document that is not in My Documents, the program all but quits responding. This is happening in Excel and Word. If I am willing to wait it will eventually give me the opportunity to search other directories. Opening a document using Windows Explorer gives me an immediate response. ...

why do word documents show up on excel and access file menu?
When you do File / Open have you get it set to 'All Files' in the open dialog box? -- Regards Ken....................... Microsoft MVP - Excel Sys Spec - Win XP Pro / XL 97/00/02/03 ---------------------------------------------------------------------------- It's easier to beg forgiveness than ask permission :-) ---------------------------------------------------------------------------- "LJH" <LJH@discussions.microsoft.com> wrote in message news:0E62222C-DEB8-4C82-8D5E-EB1F8E31BD3B@microsoft.com... > ...

double-clicking an excel file does not open it in 2003
I have a brand new Dell Precision 360 Workstation that came with Office 2003 pre-installed. I also installed Visio 2003 Standard which gave me an option for Office updates which I also completed. With Word 2003, I can open a document two ways, double-click the file or open Word and then do File, Open. But in Excel 2003 I can only open Excel then do File, Open. If Excel is closed and I double-click an Excel File, neither Excel nor the file opens. I already checked Folder Options, File Types, it correctly points to the Excel executable and has Open as the default action. Hi try t...

HyperLink and File Association
I recently learned about HYPERLINK (CNTRL-K) thanks to this NewsGroup replies. There are now several files (PDF and JPG) linked to with hyperlinks. My problem now seems to be that a jpeg file is opened with JASC software, rather than with IrfanView. No problem when opening an Adobe pdf file. How may I change the file association used by XL's hyperlinks such that IrfanView is used as default for opening jpeg images ? Regards, de ~ Vince ~ On Thu, 09 Jun 2005 18:18:47 GMT, Vince <NoSPAM2THISHAM@yahoo.com> wrote: > >I recently learned about HYPERLINK (CNTRL-K) thanks to th...

XML Serialize/Deserialize multiple objects from a single XML file.
After doing a search through google's archives of this list, I didn't see what I was looking for so here goes... Is it possible to serialize/deserialize multiple objects from a single XML file? I was looking to use the XML serialization to be able to translate complex XML elements in a single XML file through XML serialization as opposed to using SAX and coding my own serializer. However, the XML Serialization API appears to only allows for reading/writing each object as its own XML document via the XmlSerializer.Serialize method. Granted, it would be possible to use a MemoryStream...

creation of bmp files
is there a method of creating bmp files by entering (x,y) coordinates into a spreadsheet and then somehow converting to a bmp file? Are you hoping to create bmp files of charts? "c vance" <cgvance@juno.com> wrote in message news:030b01c3a583$61c5aef0$a601280a@phx.gbl... > is there a method of creating bmp files by entering > (x,y) coordinates into a spreadsheet and then somehow > converting to a bmp file? ...

xml serialization and xmlelement attribute
Hi: I have some question about the xml serialization in .NET. I have a class member defined as follows: [XmlElementAttribute("Request", typeof(FiCreditDecisionRequest), IsNullable=true)] [XmlElementAttribute("Response", typeof(FiCreditDecisionResponse), IsNullable=true)] public new object[] MessageCollection { get{ return messageCollection; } set{ messageCollection = value; } } When I serialize this class, I get the following xml: <Request xsi:type="FiCreditDecisionRequest" TypeOfRequest="CreditApplicationStatus" RequestID="1"...

Problem to load .fnt fonts
Hi, I am developing a WYSIWIG editor using .fnt fonts that I created in order to meet our product requirements. I am working on XP. Instead of putting the ..fnt fonts in the windir\fonts directory, I used the AddFontResource method to load them when the application starts. On XP and W2000, I have not problem and the RichEdit control that I am using recognizes the font. On W95/98/Me the AddFontResource doesn't work and the application failed. I tried to put directly the .fnt fonts in the windir\fonts but it failed. It seems that it only accepts .fon fonts. Even if I load this type of...

Out of date system files
Using A2K, I have created an install folder to be used for distribution to multiple users. The users have either Windows 2000 or Windows XP. On some of the machines I get a message that some of the files are out of date and need to be updated. When I take the option to do that, the install comes back to the same message, it never ends. When I first experienced a problem similar to this problem about a year or more ago, I found out that Windows 2000 needed to be on at least SP3. That is not the problem because one of the machines with this problem is Windows 2000 SP4, and has in the...

saving publisher file to PDF file format
after creating a page in publisher i need to save it as a pdf file and send by email to a publishing house >-----Original Message----- >after creating a page in publisher i need to save it as a pdf file and send >by email to a publishing house After creating a monthly newsletter in Publisher 2003, I save the file in an easy to find location on my hard drive, then, go to "www.adobe.com" and choose "create a .pdf File" from their home page. This gives you the opportunity to create up to 6 pdf's in a free trial, or to subscribe to one of their "un...

Outlook PST File Problem
I recently have begun having problems with my Outlook PST file. This has never occurred before over the many years I have been using the same Outlook file and I believe the problems started happening right around the same time that I put in iTunes onto my PC and started to allow iTunes to sync my Outlook contacts with my iPhone. My Outlook file still more or less functions normally, but each time I open Outlook I get an error message telling me that the Outlook file was not closed properly the last time it was used and it much be checked for errors. It then does the check, which often it tak...

Backup .pst files #3
I just bought a new computer and reloaded outlook. I backed up the .pst file and showed 374MB. When I applied the restore of the 2000 e mails backed up only 600 appeared. The dates for the e mail are 2002 and 2003, no 2004. Near the end of 2003 I was using XP Pro and the program became corrupt. I bought XP Home edition. Why is back up not backing up or coping. Please help. How did you backup the pst, the OL backup addin? Have you tried opening the pst in OL, rather than import. If on opening pst, mail is missing, then backup went wrong. Assuming old sys is still available copy pst to cd...

Why am I getting virus protection error when load excel file
I just upgraded to excel 2003. When I try to old or new file I've create, I get virus file detection error Hi! Have you recently run these files through a virus checker? Just thought... Al -- Message posted from http://www.ExcelForum.com Try using Tools/Options/General and uncheck Macro Virus Protection which detects any macro in a workbook - even if it is yours -- Message posted from http://www.ExcelForum.com Gary, This is ambiguous. If you get an Excel warning to the effect of "This workbook contains macros. Macros can be bad...," this is normal if the workbook...

Removing Access 97 recently used file list
Is it possible to suppress the recently used file list from Access 97? I've just discovered that even if you hide the database window and lock it so that the user can't get in by holding down the shift key, the user is still able to open the whole thing, completely unprotected, simply by selecting it twice in succession from the recently used file list on the File menu. Seems absolutely ridiculous to me... It sounds, then, as though your database isn't properly secured, as that should have no bearing on how it opens. For Access 97, the MRU is stored as up to four...

Load a toolbar with Bitmaps and menu in a Dialog Box
How to load a tool bar and menu in a Dialog Box. I did create a Dialog box when abutton is clicked . 1)I guess we need to create a variable of CToolBar and use this in Create,Right !!! 2) For Menu... Viewer = new CCVDlg(); //Check if new succeeded and we got a valid pointer to a dialog object if(Viewer != NULL) { BOOL ret = Viewer->Create(IDD_DLG_VIEWER,this); if(!ret) //Create failed. AfxMessageBox("Error creating Dialog"); Viewer->ShowWindow(SW_SHOW); } else AfxMessageBox("Error Creating Dialog Object"); Pls guid...

Need help with an excel file
I have an excel file that was generated by another program. the file has a column like theis B1 200 B2 56 B3 -1 B4 10 B5 -1 B6 100 I want to be able to do the following if the value is equal to -1 leave it alone if it is different from -1 then change the value to (value/5) Please help...i would really appreciate it. Thank you a lot Put this formula in C1: =IF(B1=-1,-1,B1/5) then copy down as far as you need to Ckana74 <Ckana74.sxh5n@excelforum.com> wrote in message news:<Ckana74.sxh5n@excelforum.com>... > I have an excel file that was generated...

Can PST imported into OST file then be merged into Exchange?
Outlook PST data from a previous Exchange mailbox was imported into a new OST file for a user that moved to a new domain and new Exchange server. If that same PST file gets merged/imported into the user's Exchange maibox on the new server will that cause those messages to get read into and duplicated in the user's current OST file? Thank you. I would suggest using Exmerge.exe to import from PST file. There is an option to create duplicates. -- John Oliver, Jr MCSE, MCT, CCNA Exchange MVP 2006 Microsoft Certified Partner "Neilrahc" <Neilrahc@discussions.microsof...

Reading XML schema into Dataset
I have following xml schema: <?xml version="1.0" encoding="utf-8" ?> <xs:schema id="XMLTestTypes" targetNamespace="http://www.geovap.cz/cityware/XMLTypes" elementFormDefault="qualified" xmlns="http://www.geovap.cz/cityware/XMLTypes" xmlns:xtypes="http://www.geovap.cz/cityware/XMLTypes" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:simpleType name="IDSubjektu"> <xs:restriction base="xs:string"> <xs:minLength value="1" /> <xs:maxLength value="50&...

Excel 97 renames file 1 character only
Hi, I'm running Excel 97 SR-2 on XP. When I do a 'save as' Excel will only save the first character of the filename. I select 'save as', enter a filename, when I click Save the filename gets truncated to only the first character. It actually does this on a new workbook too. Select New, select Save, enter a name, click Save and the name gets truncated to the first character only. TIA, Monte Odd problem. Does it occur if you start Excel in Safe Mode? (From Start->Run enter "Excel.exe /s" (without quotes, there is a space before the slash)). -- Jim ...

Publisher/Word locks up when inserting pictures from files
Inserting clip art works ok but inserting pictures from files causes Pub 2003 to lock up. Has been working fine up to some time this month. Save As also causes lock up depending on file(?). Same problem in Word 2003. Pub 2000 & Word 2000 on same computer are ok. Norton's antivirus can not find anything nor was anything indicated by spy sweeper. Uininstalled Office 2003 and re- installed - no help. Using an HP 3GHz Windows XP for home editions on a small office net with one other computer (compaq), cable modem and two printers. This has been working since last December. ...

default user files
Some time back I noticed my default user files were gone. I don't know what happened to them but they are gone. Anyhow I had read in the past that these files were needed to create new files for new user accounts, and recently I had need to create some new user accounts and I set out to find how to restore the default user files. I could not find any real definitive info on this but basically I copied the All Users files to a folder I created (in Documents and Settings) named Default User. I then deleted out some of the files within the subfolders that seemed to apply only to the...

format question when open csv file
I receive a csv file that I open in excel. The first field in this file is a nine alphanumeric character. When I open the file excel formats that field in how it sees fit. Example 1157687E6 would display in scientific notation. I make a quick update to the file and resave as .csv file. The csv file resaves that field as the scientific notation, so the people I pass this file to have problems with it. Is there a way to open a csv file in excel where excel does not reformat the field or give me an option to not reformat. >> Is there a way to open a csv file in excel where exc...