Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 659
  • Last Modified:

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

0
Tania_Farmer
Asked:
Tania_Farmer
  • 7
  • 6
  • 5
1 Solution
 
abelCommented:
What you are after is q.@title.Value, that should do the trick.
0
 
abelCommented:
Apologies, spoke to fast...
0
 
Tania_FarmerAuthor Commented:
yes, did that first.
I have definitely set it up wrong.

T
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
abelCommented:
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

0
 
Tania_FarmerAuthor Commented:
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>
0
 
abelCommented:
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

0
 
Sreedhar VengalaSr. Consultant - Business IntelligenceCommented:
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

0
 
Sreedhar VengalaSr. Consultant - Business IntelligenceCommented:
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

0
 
Tania_FarmerAuthor Commented:
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
0
 
Tania_FarmerAuthor Commented:
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      
0
 
Sreedhar VengalaSr. Consultant - Business IntelligenceCommented:
try :
<td><%#Container.DataItem %></td>
0
 
Sreedhar VengalaSr. Consultant - Business IntelligenceCommented:
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
0
 
Tania_FarmerAuthor Commented:
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
0
 
Sreedhar VengalaSr. Consultant - Business IntelligenceCommented:
here it is:

Dim list = (From x In xd.Descendants("product").Attributes("title") _
Select New With { .Title = x.Value }.ToList()  
0
 
Sreedhar VengalaSr. Consultant - Business IntelligenceCommented:
sorry missed closing parenthisis

Dim list = (From x In xd.Descendants("product").Attributes("title") _
Select New With {.Title = x.Value}).ToList()
0
 
Tania_FarmerAuthor Commented:
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
0
 
abelCommented:
Glad you solved it with the help of sree_ven, I wasn't around anymore but just saw the conclusion of the thread.
0
 
Tania_FarmerAuthor Commented:
Hi,
Thanks abel.

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

http://www.experts-exchange.com/Programming/Languages/.NET/LINQ/Q_24364944.html#a24259752

T
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now