using Gridview xpath to get xml node

  • Thread starter Thread starter kpg
  • Start date Start date
K

kpg

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 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?
 
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
 
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
 
Back
Top