using Gridview xpath to get xml node

Discussion in 'ASP .Net' started by kpg, Jan 6, 2009.

  1. kpg

    kpg Guest

    Hi all,

    I have a gridview bound to an xmldatasource using itemtemplets and xpath
    to bind the xml data to the grid.

    I have a (hidden) column in the grid that contains a particular child
    node from my data set.

    So my xml is of the form:

    <xml>
    <record>
    <data1>Some data</data1>
    <data2>Some other data</data2>
    <data3>
    <childdata1>child data</childdata1>
    <childdata2>more child data</childdata2>
    </data3>
    </record>
    </xml>


    The gridview is as follows:


    <asp:TemplateField HeaderText="col1">
    <ItemTemplate>
    <%#XPath("data1")%>
    </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="col2">
    <ItemTemplate>
    <%#XPath("data2")%>
    </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="col3">
    <ItemTemplate>
    <%#XPath("data3")%>
    </ItemTemplate>
    </asp:TemplateField>


    what I get in col3 is:

    child datamore child data

    what I want is:

    <childdata1>child data</childdata1><childdata2>more child data
    </childdata2>


    So that I can use javascript to parse the xml child node fragment and
    build a dynamic info box in response to some user event.

    Is there a way to get the outer xml instead of only the text?

    Thanks,
    kpg
     
    kpg, Jan 6, 2009
    #1
    1. Advertising

  2. kpg

    Stan Guest

    On 6 Jan, 21:06, kpg <> wrote:
    > Hi all,
    >
    > I have a gridview bound to an xmldatasource using itemtemplets and xpath
    > to bind the xml data to the grid.
    >
    > I have a (hidden) column in the grid that contains a particular child
    > node from my data set.
    >
    > So my xml is of the form:
    >
    > <xml>
    >         <record>
    >                 <data1>Some data</data1>
    >                 <data2>Some other data</data2>
    >                 <data3>
    >                         <childdata1>child data</childdata1>
    >                         <childdata2>more child data</childdata2>
    >                 </data3>
    >         </record>
    > </xml>
    >
    > The gridview is as follows:
    >
    > <asp:TemplateField HeaderText="col1">
    >         <ItemTemplate>
    >                 <%#XPath("data1")%>
    >         </ItemTemplate>
    > </asp:TemplateField>
    >
    > <asp:TemplateField HeaderText="col2">
    >         <ItemTemplate>
    >                 <%#XPath("data2")%>
    >         </ItemTemplate>
    > </asp:TemplateField>
    >
    > <asp:TemplateField HeaderText="col3">
    >         <ItemTemplate>
    >                 <%#XPath("data3")%>
    >         </ItemTemplate>
    > </asp:TemplateField>
    >
    > what I get in col3 is:
    >
    > child datamore child data
    >
    > what I want is:
    >
    > <childdata1>child data</childdata1><childdata2>more child data
    > </childdata2>
    >
    > So that  I can use javascript to parse the xml child node fragment and
    > build a dynamic info box in response to some user event.
    >
    > Is there a way  to get the outer xml instead of only the text?
    >
    > Thanks,
    > kpg


    Hi

    Are you aware that xml datasources only work with the data in
    attribute form, unless you supply a transform file?
     
    Stan, Jan 7, 2009
    #2
    1. Advertising

  3. kpg

    kpg Guest

    > Are you aware that xml datasources only work with the data in
    > attribute form, unless you supply a transform file?



    Well, I'm not sure what you mean. I am using a transform file to
    provide sorting of my source xml. If by attribute form you mean the
    text must be only in attributes? If so I must disagree in that the
    technique I presented in my post is yeilding the results I stated, and
    none of the data is in attributes - however, I'm sure I'm
    misunderstanding what you said.

    Dispite all that, I want to be able to access the data in my data3 node
    from the example including the angle brackets (that is, as xml),
    otherwise it is impossible to parse the data.

    I have tried to pass the xpath evaluation to a shared function like
    this:

    <%#MyFunction(XPath("data3"))%>

    pubilc shared function MyFunction(v as string) as string

    But there also v is only the text, not the outerXML.

    What I could do is pass a unique key to this function that ids the data
    I want from the xml, but this is a shared function so I do not have
    access to the class xmldatasource or any session variables.

    I further attempted to add the data on the rowcreated gridview event,
    This seemd promising, but I could not find a way to actually modify cell
    values from this event.

    I suppose what I could do is manually add the data to the grid, after
    databinding - is this possible?

    The only concern I have with this is that I do allow sorting and
    filtering of the grid (via xmldatasource xpath and xsl file changes) so I
    will need to re-add the data each time - it would be nice if this were
    'automated'.

    kpg
     
    kpg, Jan 7, 2009
    #3
  4. kpg

    kpg Guest

    Resolved, but using a different technique.

    I used the row databound event to retrieve the datakey, then using the
    xml from the datasource I was able to do an xpath query and get the node
    I wanted and set the cell text - this almost worked...

    Problem was since the column is hidden it is not rendered in the html. I
    knew this and thought I had the whole hidden column thing resolved
    becuase I used the technique of setting the column to hidden in the row
    created event - but alas it was not part of the html (I assumed it would
    be there with a display attribute of none). This technique did seem to
    bind the data to the hidden column though in that the data was in the
    viewstate (which grew considerably).

    So while the data was represented in the viewstate, I figured what the
    heck, and I simply added the data to an 'array' of hiddend fields -
    which are eaisly accessed by my javascript later.

    But the hidden column issue is really a side bar - I essentially wanted
    a data island type of structure that I could use client side to build a
    detail box when the user hovers on a grid row for a time.

    Using the hidden fields built dynamically as rows are data bound
    accomplishes this task - but I suspect its a bit of a hack.

    I could have used a web service call or some some ajax style server
    query, but I wanted the grid to be responsive - I am quite pleased with
    the end result, however.

    kpg
     
    kpg, Jan 7, 2009
    #4
    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. Replies:
    0
    Views:
    1,620
  2. Marvin_123456

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

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    2,010
    jan V
    Jul 29, 2005
  3. Alastair Cameron
    Replies:
    1
    Views:
    7,480
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  4. Murtaza Tinwala
    Replies:
    1
    Views:
    739
    Martin Honnen
    Sep 20, 2004
  5. Tjerk Wolterink
    Replies:
    2
    Views:
    1,484
    Dimitre Novatchev
    Aug 24, 2006
Loading...

Share This Page