Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving to a Specific XML Element

Status
Not open for further replies.

MISdad

Technical User
May 5, 2004
34
I need to be able to save uploaded file names to a specific XML Element. Here's where I am...

My XML File
Code:
<?xml version="1.0" encoding="utf-8"?>
<photoalbum>
  <album albumname="PhotoAlbum1" id="1">
    <image id="1" imagename="pic1.jpg" />
    <image id="2" imagename="pic2.jpg" />
    <image id="3" imagename="pic3.jpg" />
  </album>
  <album albumname="PhotoAlbum2" id="2">
    <image id="1" imagename="pic1.jpg" />
    <image id="2" imagename="pic2.jpg" />
    <image id="3" imagename="pic3.jpg" />
  </album>
</photoalbum>

I can currently insert images but they are all going under the first album. I need to be able to specify which album the new images get put into based on a text box entry.

I have the following C# code which adds the new image...
Code:
Xelement newElement =
new XElement("image",
new XAttribute("imagename", uploadedFileName.ToString()));

parentElement.Element("album").Add(newElement);
rootElement.Save(filename);

What I need to know is how to specify the correct albumname attribute (ie. PhotoAlbum1 or PhotoAlbum2) along with the album Element that newElement is being added to.
 
select the element using xpath... W3C schools has a good reference/tutorial... using the XML you provided above, your code will look something like this (if you wanted to select the first album)....

Code:
System.Xml.XmlNode node = xmlDoc.SelectSingleNode("/photoalbum/album[@albumname='PhotoAlbum1']");

Hope this helps.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top