Need help with xpath and xml

Discussion in 'ASP .Net' started by Showjumper, Jul 24, 2007.

  1. Showjumper

    Showjumper Guest

    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>
     
    Showjumper, Jul 24, 2007
    #1
    1. Advertising

  2. 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" wrote:

    > 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>
    >
    >
    >
     
    =?Utf-8?B?TWFuaXNoIEJhZm5h?=, Jul 25, 2007
    #2
    1. Advertising

  3. Showjumper

    Showjumper Guest

    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" <> wrote in message
    news:D...
    > 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" wrote:
    >
    >> 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>
    >>
    >>
    >>
     
    Showjumper, Jul 25, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    2,028
    jan V
    Jul 29, 2005
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,525
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. Anna
    Replies:
    0
    Views:
    572
  4. goog
    Replies:
    0
    Views:
    536
  5. Tjerk Wolterink

    XPath: efficiency in xpath expressions

    Tjerk Wolterink, Nov 13, 2004, in forum: XML
    Replies:
    1
    Views:
    1,704
    Richard Tobin
    Nov 13, 2004
Loading...

Share This Page