Solved

Load ListBox from XML using LINQ

Posted on 2012-03-27
8
409 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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 74

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 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

15 Experts available now in Live!

Get 1:1 Help Now