Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Load ListBox from XML using LINQ

Posted on 2012-03-27
8
Medium Priority
?
425 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 2000 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
Amazon Web Services EC2 Cheat Sheet

AWS EC2 is a core part of AWS’s cloud platform, allowing users to spin up virtual machines for a variety of tasks; however, EC2’s offerings can be overwhelming. Learn the basics with our new AWS cheat sheet – this time on EC2!

 

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 2000 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

719 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