Solved

Load ListBox from XML using LINQ

Posted on 2012-03-27
8
424 Views
Last Modified: 2012-03-28
How do I read the following xml using LINQ?  I want to load a ListBox with TheText and ID and filter the query on WeekOf.

Here's what I have, but it does not work.

         Dim recs As XElement = XElement.Load(MapPath(APP_DATA & "WFTP.xml"))

         For Each c In recs.Descendants("Table1")
            Me.lstPastWFTPs.Items.Add(c.@TheText, c.@ID)
         Next

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <Table1>
    <ID>1</ID>
    <TheText>This is record 1</TheText>
    <WeekOf>2010-01-03T00:00:00-05:00</WeekOf>
    <Title>Record 1</Title>
  </Table1>
  <Table1>
    <ID>2</ID>
    <TheText>This is record 2</TheText>
    <WeekOf>2010-01-03T00:00:00-05:00</WeekOf>
    <Title>Record 2</Title>
  </Table1>
</NewDataSet>
0
Comment
Question by:Ray Turner
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 20

Assisted Solution

by:BuggyCoder
BuggyCoder earned 500 total points
ID: 37775053
Me.lstPastWFTPs.Items.Add(c.Descendants("TheText").First().Value, c.Descendants("ID").First().Value)

Open in new window


or

Me.lstPastWFTPs.Items.Add(c.Element("TheText").Value, c.Element("ID").Value)

Open in new window

0
 

Author Comment

by:Ray Turner
ID: 37775077
Thanks!  That's perfect.  One more question.  How would I filter based on WeekOf?

Something like:

SELECT * FROM aaa
WHERE WeekOf between a and b
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37775116
string weekOff= c.Element("WeekOff").Value;
DateTime wOff=Convert.ToDateTime(weelOff);

do the comparison using wOff DateTime variable....
0
Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

 

Author Comment

by:Ray Turner
ID: 37775137
How would do this using a LINQ query expression?

dim q = From blah, blah
              Select
0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 37775147
var list= resc.Descendents("Table1").Where(c=>Convert.ToDateTime(c.Element("WeekOff").Value) > <yourDateTime>).Select(c=>new {ID=c.Element("ID").Value, Text=c.Element("TheText").Value}).ToList();

This will contain list of anonymous type with ID and Text as string.
You can then iterate over this list as per your requirement.
0
 

Author Closing Comment

by:Ray Turner
ID: 37776061
Thanks!
0
 

Author Comment

by:Ray Turner
ID: 37776101
Can you show me a VB version?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37776105
This is precisely the kind of thing that data binding is for. You could, for example, do something like this in your code-behind:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Me.ListBox1.DataSource = From table1 In XElement.Load(MapPath(APP_DATA & "WFTP.xml")).Descendants("Table1") _
                             Let convertedDate = Convert.ToDateTime(table1.Element("WeekOf").Value)
                             Let minimumDate = New DateTime(2010, 1, 1)
                             Let maximumDate = New DateTime(2010, 1, 5)
                             Where convertedDate >= minimumDate AndAlso convertedDate <= maximumDate
                             Select New With
                             {
                                .ID = table1.Element("ID").Value,
                                .Text = table1.Element("TheText").Value
                             }

    Me.ListBox1.DataTextField = "Text"
    Me.ListBox1.DataValueField = "ID"
    Me.ListBox1.DataBind()

End Sub

Open in new window


The strings you assign to both the DataTextField and the DateValueField should correspond to the identity you provided within the new anonymous type (e.g. the ".ID" and ".Text" pieces in my example above).
0

Featured Post

Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question