Click here to Skip to main content
15,924,507 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,
I have a xml file like.

XML
<?xml version="1.0" encoding="utf-8"?>
<config>
	<galleries>
		<folder name="Nature">
			<gallery name="Landscapes">
				<img src="nature01.jpg" width="351" height="550" caption="Some photo" />
				<img src="nature02.jpg" width="559" height="420" caption="Some photo" />
			</gallery>
			
			<gallery name="Water">
				<img src="nature01.jpg" width="351" height="550" caption="Some photo" />
				<img src="nature02.jpg" width="559" height="420" caption="Some photo" />
			</gallery>
			<gallery name="Mars">
				<img src="mars01.jpg" width="431" height="565" caption="Some photo" />
			</gallery>
		</folder>
		<folder name="Constructs">
			<gallery name="Buildings">
				<img src="construct01.jpg" width="328" height="500" caption="Some photo 1" />
				<img src="construct02.jpg" width="323" height="500" caption="Some photo 2" />
			</gallery>
			<gallery name="Installations">
				<img src="construct01.jpg" width="328" height="500" caption="Some photo" />
			<img src="construct02.jpg" width="323" height="500" caption="Some photo" />		
			</gallery>
		</folder>
		<gallery name="Venice">                                    
			<img src="venice01.jpg" width="448" height="418" caption="Some photo" />
		</gallery>
</galleries>
	<section name="About us" width="300" height="420" bydefault="true">
		<title>Full dynamic website demo</title>
    <p>Demo Demo</p>
</section>
	<titleandslogan>
		<p class="ptitleandslogan"><span class="spantitle">Delight</span><br />photography studio</p>
	</titleandslogan>
</config>


i have to dynamically Add & Update the images inside the gallery and the section also i am new in development how can i do it plz help.
Posted
Updated 31-Jul-12 2:11am
v2

1 solution

Following are some of the options to do the same

1. Using XmlDocument
C#
XmlDocument doc = new XmlDocument();
doc.Load("D:\\build.xml");
XmlNode root = doc.DocumentElement; 
XmlNode myNode = root.SelectSingleNode("galleries::folder");
myNode.Value = "blabla";
doc.Save("D:\\build.xml");

2. You can also use LINQ to do the same
C#
using System.Xml.Linq;  
XDocument xmlFile = XDocument.Load("D:\\build.xml");  
var query = from c in xmlFile.Elements("galleries").Elements("folder")                 
select c;  foreach (XElement folder in query)  
{
	folder.Attribute("attr1").Value = "MyNewValue"; 
} 
xmlFile.Save("build.xml"); 
 
Share this answer
 
Comments
Umashankar Yadav 31-Jul-12 8:42am    
Thanks Om Prakash Pant Ji

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