XMLDocument 'Save' Method fails with 'Permission Denied'

C

Carl Williams

Hope someone can help with this...

I have looked at all the newsgroup articles and put into practice all
the suggestions but to no good. I am pretty new to CSharp and .Net so
any help would be greatly appreciated.

I have an .ASPX.CS in which I am trying to open an XML file (which
already contains content), making some alterations to the XML obtained
from the file, and then trying to 'Save' the XML back to the file. I
am using a StreamReader object to obtain the XML string from the file,
I then load the XML into an XMLDocument object to make the necessary
changes to the data within the XML. Once that operation is complete I
open a further XMLDocument and load in the altered XML. I then try to
perform a 'Save' operation back to the originating XML file, but it
fails saying 'Access to [XML File Path] is denied'. The File is
opened and saved using HttpContext.Current.Server.MapPath()

I have made sure that ASP.NET has the required permissions on the
file, and I know this works as I can write the file back with a
different filename. Some suggestions on the groups have said to use
'serialization'. To essentially 'serialize' the XML file contents to
an object, make changes to the objects data, and then 'de-serialize'
it to persist it back to the XML file.

Can anyone offer a simple example to do this, and explain why it has
to be this way in order to be able to open, process, and save back an
XML file?

I have tried moving the location of the file to differing folders, and
have tried different names etc.

Many Thanks in advance.

Carl Williams
(e-mail address removed)
 
G

Greg Merideth

First guess would be that with ntfs the permissions write and change are
two different permission's. If you can create files but not save over
the top of them you might have write access to the file but not
change(modify) access to the file onces it's been created. Easiest way
to test is to give asp.net full control over the file and the path
making sure that the permissions propagate downwards.
 
J

Joey Calisay

Their suggestion is correct, "serialization" security permission should be
enabled for that context. It is needed since the streamreader I believe
demands for this permission to be able to deserialize whatever is in your
xml be it in soap or binary format
 
T

Trebek

I'd also check to make sure your StreamReader doesn't still have a ref to
the original file in question. You can run into problems if you haven't
closed the StreamReader before attempting to 'overwrite' the file.

Alex

Greg Merideth said:
First guess would be that with ntfs the permissions write and change are
two different permission's. If you can create files but not save over
the top of them you might have write access to the file but not
change(modify) access to the file onces it's been created. Easiest way
to test is to give asp.net full control over the file and the path
making sure that the permissions propagate downwards.

Carl said:
Hope someone can help with this...

I have looked at all the newsgroup articles and put into practice all
the suggestions but to no good. I am pretty new to CSharp and .Net so
any help would be greatly appreciated.

I have an .ASPX.CS in which I am trying to open an XML file (which
already contains content), making some alterations to the XML obtained
from the file, and then trying to 'Save' the XML back to the file. I
am using a StreamReader object to obtain the XML string from the file,
I then load the XML into an XMLDocument object to make the necessary
changes to the data within the XML. Once that operation is complete I
open a further XMLDocument and load in the altered XML. I then try to
perform a 'Save' operation back to the originating XML file, but it
fails saying 'Access to [XML File Path] is denied'. The File is
opened and saved using HttpContext.Current.Server.MapPath()

I have made sure that ASP.NET has the required permissions on the
file, and I know this works as I can write the file back with a
different filename. Some suggestions on the groups have said to use
'serialization'. To essentially 'serialize' the XML file contents to
an object, make changes to the objects data, and then 'de-serialize'
it to persist it back to the XML file.

Can anyone offer a simple example to do this, and explain why it has
to be this way in order to be able to open, process, and save back an
XML file?

I have tried moving the location of the file to differing folders, and
have tried different names etc.

Many Thanks in advance.

Carl Williams
(e-mail address removed)
 
C

Carl Williams

Thanks a lot for the reply. It came down to permissions on the file in
the end.
 

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