Link to home
Start Free TrialLog in
Avatar of Member_2_4426104
Member_2_4426104

asked on

listview xml-linqdatasource /need help - data not showing

Hi,
 
I am just trying to get data into my list view from my xml file - any data; which its not.

When I use title = q.@title.text I get this error:
BC30456: 'text' is not a member of 'String'.

And when I use title = q.@title the the listview is empty.

I was using xmldatasource but I can't sort the list view with it.  So switched to linqdatasource but having a hard time getting it to work.  


Thanks
Tania

 


<script runat="server"> 
	Protected Sub listsource_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) Handles listsource.Selecting
 
		 
Dim xd As XDocument = XDocument.Load(Server.MapPath("data.xml"))
Dim query = From q In xd...<courseList>.<category>.<product> _
		     Select _ 
		  		title = q.@title.text
  		    e.Result = query		
  
    End Sub
 
</script>
 
<asp:LinqDataSource 
      		ID="listSource" 
      		runat="server" 
      		OnSelecting="listsource_Selecting" />
 
 
<asp:ListView ID="dates" runat="server" DataSourceID="listsource"  >
          <LayoutTemplate>
            <div id="datatable">
              <table border="1" class="prodtbl" runat="server">
                <tr class="mainheader">
                  <th > <asp:LinkButton ID="btnSortName" 
                       runat="server" CommandName="Sort" 
                       CommandArgument="title" Text="Course" />
                  </th>
                  <th> <asp:LinkButton ID="btnSortDuration" 
                     runat="server" CommandName="Sort" 
                     CommandArgument="title" Text="Duration" />
                  </th>
                  <th> <asp:LinkButton ID="btnSortDate" 
                      runat="server" CommandName="Sort" 
                      CommandArgument="title" Text="Date" />
                  </th>
                </tr>
                <tr id="itemPlaceholder" runat="server" /></tr>
              </table>
              <div class="pager">
                <asp:DataPager ID="pagerTop" runat="server" PageSize="20">
                  <Fields>
                    <asp:NextPreviousPagerField 
                         ButtonCssClass="command" 
                         FirstPageText="&laquo;" PreviousPageText="&lsaquo;" 
                         RenderDisabledButtonsAsLabels="true" 
                         ShowFirstPageButton="true" ShowPreviousPageButton="true" 
                         ShowLastPageButton="false" ShowNextPageButton="false"
                                            />
                    <asp:NumericPagerField
                         ButtonCount="5" NumericButtonCssClass="command" 
                         CurrentPageLabelCssClass="current" NextPreviousButtonCssClass="command"
                                            />
                    <asp:NextPreviousPagerField 
                         ButtonCssClass="command" 
                         LastPageText="&raquo;" NextPageText="&rsaquo;" 
                         RenderDisabledButtonsAsLabels="true" 
                         ShowFirstPageButton="false" ShowPreviousPageButton="false" 
                         ShowLastPageButton="true" ShowNextPageButton="true"
                                            />
                  </Fields>
                </asp:DataPager>
              </div>
            </div>
          </LayoutTemplate>
          <ItemTemplate>
            <tr id="row" >
               <td><%# Eval("title") %></td>
               <td><%# Eval("title") %></td> 
              <td><%# Eval("title") %></td>
            </tr>
          </ItemTemplate>
        </asp:ListView>

Open in new window

Avatar of abel
abel
Flag of Netherlands image

What you are after is q.@title.Value, that should do the trick.
Apologies, spoke to fast...
Avatar of Member_2_4426104
Member_2_4426104

ASKER

yes, did that first.
I have definitely set it up wrong.

T
Can you show bits of the XML please? I think your query may be wrong and you just don't select anything.

Why do you do "title = q.@title"? This should be enough to get a query filled with strings:

       Dim query = From q In xd...<courseList>.<category>.<product> Select q.@title

Because after I get the one thing working was going to expand it to select more items.
I had just cut it back completely as I couldn't get it to work.

<courseList filedUnder="Courses">
  <category code="4wd" title="4wd Operation" >
    <product code="lr" title="4wd Bush Driving">
      <duration>1 Day</duration>
    </product>
    <product code="hr" title="4wd Defensive Driving">
      <duration>1 Day</duration>
    </product>
    <product code="sr" title="Self-Recovery">
      <duration>1 Day</duration>
    </product>
  </category>
</courseList>
Well, I think you are quite close. Can you try to copy the following snippet? Because that works with your data and I just tried it and it outputs the following for me, which is, I believe, what you wanted:
4wd Bush Driving4wd Defensive DrivingSelf-Recovery
        Dim xd As XDocument = XDocument.Parse("<courseList filedUnder='Courses'>" & _
          "<category code='4wd' title='4wd Operation' >" & _
            "<product code='lr' title='4wd Bush Driving'>" & _
              "<duration>1 Day</duration>" & _
            "</product>" & _
            "<product code='hr' title='4wd Defensive Driving'>" & _
              "<duration>1 Day</duration>" & _
            "</product>" & _
            "<product code='sr' title='Self-Recovery'>" & _
              "<duration>1 Day</duration>" & _
            "</product>" & _
          "</category>" & _
        "</courseList>")
 
        Dim query = From q In xd...<courseList>.<category>.<product> _
                             Select q.@title
 
        For Each title As String In query
            Debug.WriteLine(title)
        Next

Open in new window

Try this out:
 

XDocument xd = XDocument.Load(@"c:\linqsource.xml"); 
var titleList = (from t in xd.Descendants().Attributes("title") select t.Value).ToList(); 

Open in new window

Note: Above will bring all the values of Attribute 'title' in xml.
if Need titles of <product>
this will do:

 XDocument xd = XDocument.Load(@"c:\linqsource.xml");
            var titleList = (from t in xd.Descendants("product").Attributes("title") select t.Value).ToList();

Open in new window

Hi sree_ven,
I get this error:
System.Web.HttpException: DataBinding: 'System.String' does not contain a property with the name 'title'.

on this line:
<td><%# Eval("title") %></td>

T
Hi abel,
I tried this:
Dim query = From q In xd...<courseList>.<category>.<product> Select q.@title
e.Result = query

 and also had a problem at: <td><%# Eval("title") %></td>

same error as above.

On the site - if I use:
 For Each title As String In query
           title.
       Next      

What expression do I use to return "title"?

Thanks
Tania      
try :
<td><%#Container.DataItem %></td>
Ok Tania
This should do for :

XDocument xd = XDocument.Load(@"c:\courseList.xml");
var list = (from x in xd.Descendants("product").Attributes("title") select new { Title= x.Value}).ToList();
e.Result = list;

<tr id="row" > 
<td><%# Eval("title") %></td>
hope this helps you
Hi,
<td><%#Container.DataItem %></td> worked.

For the second option, what's the conversion for --- select new { Title= x.Value}).?

   Dim list = (From x In xd.Descendants("product").Attributes("title") _
   Select New (                )).ToList()
 
   e.Result = list

Thanks
Tania
here it is:

Dim list = (From x In xd.Descendants("product").Attributes("title") _
Select New With { .Title = x.Value }.ToList()  
ASKER CERTIFIED SOLUTION
Avatar of Sreedhar Vengala
Sreedhar Vengala
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yes - it works!

Thanks you.

I have another question which I will post as a new question.

How do expand the list so:

Dim list = (From x In xd.Descendants("product").Attributes("title") _
Select New With {.Title = x.Value}).ToList()

to include @title and <duration>.

Thanks
Tania
Glad you solved it with the help of sree_ven, I wasn't around anymore but just saw the conclusion of the thread.
Hi,
Thanks abel.

One part solved - but for some reason still having problems when expanding the select options:

https://www.experts-exchange.com/questions/24364944/listview-linq-expand-selection.html?anchorAnswerId=24259752#a24259752

T