Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
I embedded a xml file in Resource files.
In programme code, I need to read this xml and edit the content, then save it to Resource file.
But how to save the changed content to Resource files?
Posted 3-Nov-10 20:44pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi Kean,
 
You cant do this. Resource files are compiled in a special format and attached in your application. See this http://msdn.microsoft.com/en-us/library/f45fce5x(v=VS.80).aspx. I think you will find it usefull.
 
If you want to edit an xml file during runtime, dont embed it in resources. Store it anywhere on your disk and access that path.
  Permalink  
Comments
Dalek Dave at 4-Nov-10 4:51am
   
Good Call.
Kean(cn) at 4-Nov-10 6:32am
   
Thank you dmageiras!
And I had tried another way: I add a xml file to the Resource files(embedded), then run the programme, use script to create a new Resource file to overide orignal xml file.. but fail finally.
 
Seems I should give up the action of modifying Resource file..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi kean.. i was also having the same problem that you were facing. i had also posted questions on CodeProject earlier but had no success. Finally after doing a lot of "google" and books,what i found is that :
 
1. You cannot edit an Embedded Resource.
 
2. The XML file should not be kept as a resource as said by dmageiras. So what you can do is either
a)store it in some disk and access
or do it in my way :
b)make the Build Action of your XML to Content and then change the Copy
to Output Directory option to Copy Always.
with this the XML file will automatically go to the debug folder and
accessing XML will be much simpler:
to Load:
ie.
 XmlDocument xmlDoc=new XmlDocument();
       xmlDoc.Load("myXmlFile.Xml");
to Save:
xmlDoc.Save("myXmlFile.xml");

It is also recommended that if you want to edit any resource, it is better to keep it as Content File. After reading a number of books, the authors have warned that never keep your resource as Embedded Resource.
 
You can also have a look at this MSDN link :
http://msdn.microsoft.com/en-us/library/bbwz4bhx%28v=VS.80%29.aspx
 
Another link which will be very helpful! :
http://forums.silverlight.net/forums/p/152118/339496.aspx
 
I hope now you know what the correct way is!
 
make a simple project and try to do it. after you run your project for first time, you can check in the debug folder of your project where you can find your XML. make some changes through code on that XML and it gets reflected in that XML itself.
 
Good Luck! Smile | :)
  Permalink  
v3
Comments
Kean(cn) at 4-Nov-10 6:33am
   
Thanks very much!
And I had tried another way: I add a xml file to the Resource files(embedded), then run the programme, use script to create a new Resource file to overide orignal xml file.. but fail finally.
 
Seems I should give up the action of modifying Resource file..
Tarun.K.S at 4-Nov-10 7:12am
   
You just have to set its build action to Content! then you can easily edit or modify!!i have made a project using XML and modified it too.
Tarun.K.S at 4-Nov-10 7:41am
   
You can also have a look at this link too : http://www.codeproject.com/KB/WPF/WPFandXML.aspx
Kean(cn) at 5-Nov-10 2:51am
   
"build action to Content"..so will user could see the xml? I dont want user see/find it in programme file.
But i allow user could export and import this xml file.
 
Sorry, now my VS version is 2005, cant read the file of link
Tarun.K.S at 5-Nov-10 4:18am
   
no the user wont be able to see or find it.
Kean(cn) at 8-Nov-10 19:33pm
   
ok, thank you Tarun, but now i'm working on other function, will test this later :)
Kean(cn) at 11-Nov-10 5:05am
   
Hi Tarun,
I read your method and test some steps, but i dont understand this part:
""""
change the Copy to Output Directory option to Copy Always.
with this the XML file will automatically go to the debug folder and
accessing XML will be much simpler:
to Load:
ie.
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load("myXmlFile.Xml");
 
to Save:
xmlDoc.Save("myXmlFile.xml");
""""
where is the xml file? in Debug folder? if so the user could see it, isnt it?
 
about this link: http://forums.silverlight.net/forums/p/152118/339496.aspx
i think this question is very close to mine, but my script was stop by the path of the xml file either.
 
Could you help to specify the step of setting the xml file and its location, or show me a small case? :)
Kean(cn) at 11-Nov-10 8:38am
   
Sorry that my enveriment is Visual C#, not Web...

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

  Print Answers RSS
0 Nirav Prabtani 268
1 OriginalGriff 217
2 Mika Wendelius 185
3 _Amy 170
4 CPallini 170
0 Nirav Prabtani 284
1 OriginalGriff 217
2 Mika Wendelius 185
3 _Amy 170
4 CPallini 170


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 4 Nov 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100