Solved

VB 2010:  Data from xml... How do I query the data?

Posted on 2010-09-12
13
617 Views
Last Modified: 2012-08-14
My application uses a single (large) xml file as the source for many different reports.  My on_load sub reads the xml into a dataset, and sets a datagridview.datasource to the dataset.

Here's a very simple table in order to explain my question.  Let's say that this is what my datagridview looks like:

Name  Type  Addr
fred      A         123
wilma  B         456
barney C         789

The user then has many different options utilizing the data.  For example:  I need to find all of Type:B, then do something with Name and Addr.

So here are my questions:
1.) Should I query the dataset, or the datagridview, or is there a better approach?
2.) Depending on answer to #1... How?

Can sql query syntax be used somehow?  I know how to run through all the rows in the datagridview, and this is how I've been doing it (but think there must be a better approach):

for r=0 to datagridview1.rowcount-1
  if datagridview.item(1,r).value="B" then
    <do_something>
  end if
next
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        DataSet1.ReadXml("c:\Test.xml")
        DataGridView1.DataSource = DataSet1
End Sub

Open in new window

0
Comment
Question by:motokent
[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
  • 6
  • 5
  • 2
13 Comments
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33656669
try this
For Each row As DataGridViewRow In Me.DataGridView1.Rows
            If Not row.IsNewRow Then
                If row.Cells("Type").FormattedValue = "B" Then
                    do what you want
                End If
            End If
        Next

Open in new window

0
 
LVL 18

Assisted Solution

by:John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou earned 200 total points
ID: 33656688
Or for example
For Each row As DataGridViewRow In Me.DataGridView2.Rows
            If Not row.IsNewRow Then
                If row.Cells("Type").FormattedValue = "B" Then
                    row.Cells("Addr").Style.BackColor = Color.Azure
                End If
            End If
        Next

Open in new window

0
 

Author Comment

by:motokent
ID: 33656709
This is good, but what I'd like to know... is the datagridview approach (your suggestions, and the way I've been doing it) the best appproach?  I've seen things like LINQ to XML where I might be able to avoid datagridview totally, but not sure if there are any benefits to this (such as speed, or simplicity).
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33656720
there is no problem about the performance ...i am using this kind of approach many times,without problem ...
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 33656841
Hi motokent;

In answer to your questions:
1.) Should I query the dataset, or the datagridview, or is there a better approach?

    Seeming you are using Visual Studio 2010 I would use Linq to DataSet to do what
    you need.
   
2.) Depending on answer to #1... How?

    See code snippet below.
   
Can sql query syntax be used somehow?  

    Linq has a form that has a syntax of SQL like, see snippet.

Fernando
' This query assumes that the DataTable in question is in the 
' DataSet table zero. Query's the DataTable for all record with
' Field name Type equal to B
Dim queryTypes = From t In ds.Tables(0).AsEnumerable()
                 Where t.Field(Of String)("Type") = "B"
                 Select New With {
                     .Name = t.Field(Of String)("Name"),
                     .Type = t.Field(Of String)("Type"),
                     .Addr = t.Field(Of Int32)("Addr")
                 }

For Each t In queryTypes
    ' Process all Type B records here.
    Console.WriteLine("Name {0}  :  Type {1}  :  Addr {2}", t.Name, t.Type, t.Addr.ToString())
Next

Open in new window

0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 300 total points
ID: 33656849
Hi motokent;

If you do not need the DataGridView, DataSet and DataTable you can directly query the XML with Linq to XML.

Fernando
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33656985
Respect !!:)
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 33657017
@jtoutou, don't understand your last post?
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33657064
@Fernando  i just like and agree with  your comments...
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 33657073
@jtoutou, OK now I understand and thank you.  ;=)
0
 

Author Closing Comment

by:motokent
ID: 33657587
Thanks to both of you.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 33657605
Not a problem, glad I was able to help.  ;=)
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33657610
very very welcome!!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

751 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