Solved

Load ListBox from XML using LINQ

Posted on 2012-03-27
8
413 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
  • 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
 

Author Comment

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

dim q = From blah, blah
              Select
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now