Click here to Skip to main content
16,016,643 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am having an xml file and added that as a Resource.
How can i read and update that xml programmatically.

What I have tried:

My xml file is: Resources.BookXML
<book><mybook><price>100</price></mybook></book>

I can read the value of an element from that file like
XDocument xml = XDocument.Parse(Resources.BookXML);
string strPrice = xml.Root.Element("myBook").Element("Price").Value;

but i am not able to update that file.Is there any way to do that?
For saving i tried:
XDocument xml = XDocument.Parse(Resources.BookXML, LoadOptions.PreserveWhitespace);
xml.Root.Element("myBook").Element("Price").Value = "200";
xml.save(Resources.BookXML);

But didn't work :(
Posted
Updated 7-Dec-20 3:10am

No. Resources are read only, because they are embedded into the EXE file and do not exist as "normal files". Even if you could do it, your anti-virus software would be all over you, and they track file changes and alert when an exe gets modified.

There is also the problem that in production the Program Files folder and it's subdirectories is read only, to reduce virus activity and your app would require elevation to write to the file, even if instead of embedding them you add them as files that are installed in the app folder by the installer.

Instead, have your code check if the file exists in a safe place on start up, and if it doesn't then copy the resource to it. Then use that file, and write your revised data into the "safe location" copy. If you have a look here it explains where to store such data: Where Should I Store My Data?[^]
 
Share this answer
 
Comments
Member 14978771 7-Dec-20 9:14am    
Thank You
OriginalGriff 7-Dec-20 9:44am    
You're welcome!
As was already explained to you in your previous question[^], you cannot modify an embedded resource at runtime.

If you need to modify the file, then you cannot store it in an embedded resource.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900