Need help with xpath and xml

S

Showjumper

Hi i posted this on the dotnet xml group but that group does not get much
traffic so i thought to post here.

I have a photo gallery that works by looping thru the images, making an xml
file and then binding it to the datalist. The gallery part works fine. My
next task was to make an admin page so that captions for each of the photos
could be edited. In the edititemtemplate i have a textbox for the caption
edit and 2 linkbuttons. Using the following syntax i can locate a specific
node and edit the caption. My main problem is that it only works when the ID
is hardcoded. Only issue i have right now is how to pass in the ID
dynamically based on which image is clicked. The xml structure is below as
well. The other alternative xml structure i used was to have the ID its own
element as <PhotoID>1 </PhotoID> as an example. In this case i could set the
DataKeyField to PhotoID but i was not able to edit the Caption node using
this structure - i had issue with the xpath syntax.. I was able to edit the
caption if the ID was an attribute of the Photo element. Just need some help
in making htis work. Thanks in advance Ashok
Dim nodeList As XmlNodeList =
xmlDoc.SelectNodes(String.Format("/Photos/Photo[@ID='{0}']", ID))

nodeList(0).ChildNodes(0).InnerText = Caption

xmlDoc.Save(filepath)



<Photos>

<Photo ID="1">

<Caption>FGHFGHDFG</Caption>

<FileName>DSC00067.jpg</FileName>

<FilePath>~/photos/Stills/DSC00067.jpg</FilePath>

<ThumbnailFilePath>~/photos/Stills/thumbnails/DSC00067.jpg</ThumbnailFilePath>

</Photo>

<Photo ID="2">

<Caption>FGHFGHDFG</Caption>

<FileName>DSC00068.jpg</FileName>

<FilePath>~/photos/Stills/DSC00068.jpg</FilePath>

<ThumbnailFilePath>~/photos/Stills/thumbnails/DSC00068.jpg</ThumbnailFilePath>

</Photo>

</Photos>
 
S

Showjumper

Thanks but i am still unclear on how to use this. Also i think part of my
issue is how to do i set the datakeyfield of the datalist so that i can get
this ID and pass it into my update sub.

Ashok
Manish Bafna said:
Hi,
Try this code:
string xpath = "//("/Photos/Photo[@ID =$ID]";
XPathExpression expr = DynamicContext.Compile(xpath);
OR
//Run-time
DynamicContext ctx = new DynamicContext();
ctx.AddVariable("ID", txtID.Text);
expr.SetContext(ctx);
XPathNodeIterator custData = customers.Select(expr);
ref:http://weblogs.asp.net/cazzu/archive/2003/10/07/30888.aspx
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.



Showjumper said:
Hi i posted this on the dotnet xml group but that group does not get much
traffic so i thought to post here.

I have a photo gallery that works by looping thru the images, making an
xml
file and then binding it to the datalist. The gallery part works fine. My
next task was to make an admin page so that captions for each of the
photos
could be edited. In the edititemtemplate i have a textbox for the caption
edit and 2 linkbuttons. Using the following syntax i can locate a
specific
node and edit the caption. My main problem is that it only works when the
ID
is hardcoded. Only issue i have right now is how to pass in the ID
dynamically based on which image is clicked. The xml structure is below
as
well. The other alternative xml structure i used was to have the ID its
own
element as <PhotoID>1 </PhotoID> as an example. In this case i could set
the
DataKeyField to PhotoID but i was not able to edit the Caption node using
this structure - i had issue with the xpath syntax.. I was able to edit
the
caption if the ID was an attribute of the Photo element. Just need some
help
in making htis work. Thanks in advance Ashok
Dim nodeList As XmlNodeList =
xmlDoc.SelectNodes(String.Format("/Photos/Photo[@ID='{0}']", ID))

nodeList(0).ChildNodes(0).InnerText = Caption

xmlDoc.Save(filepath)



<Photos>

<Photo ID="1">

<Caption>FGHFGHDFG</Caption>

<FileName>DSC00067.jpg</FileName>

<FilePath>~/photos/Stills/DSC00067.jpg</FilePath>

<ThumbnailFilePath>~/photos/Stills/thumbnails/DSC00067.jpg</ThumbnailFilePath>

</Photo>

<Photo ID="2">

<Caption>FGHFGHDFG</Caption>

<FileName>DSC00068.jpg</FileName>

<FilePath>~/photos/Stills/DSC00068.jpg</FilePath>

<ThumbnailFilePath>~/photos/Stills/thumbnails/DSC00068.jpg</ThumbnailFilePath>

</Photo>

</Photos>
 

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

Members online

Forum statistics

Threads
474,038
Messages
2,570,372
Members
47,018
Latest member
IrisN51866

Latest Threads

Top