Solved

Load ListBox from XML using LINQ

Posted on 2012-03-27
8
422 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
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 

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

Technology Partners: 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!

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 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