Show selected xml in gridview from linq query

Posted on 2013-05-25
Last Modified: 2013-05-25
I have this code return all elements of each "line" element in  "InvoiceNum". But returns each one at a time. I need to return all elements of each line (ProductId, Description,Quantity) to a unique row and then show in datagridview.

				<Description>Product 1</Description>
				<Description>Product 1</Description>
				<Description>Product 2</Description>

Open in new window

Dim xdoc As XDocument = XDocument.Load("C:\Test\test.xml")

        Dim query = (From xe In xdoc.Descendants("Invoice") _
          Where (xe.Element("InvoiceNum").Value = "154") _
          From Elemento In xe.Elements("Line").Descendants _
          Select Elemento.Value).ToList

        If query.Any() Then
            For Each Elemento In query
        End If

Open in new window

Question by:rflorencio
LVL 75

Accepted Solution

käµfm³d   👽 earned 500 total points
ID: 39196527
You are selecting a String in the Select of your query. When you dynamically bind to a DGV, the DGV will locate all of the public properties of the class comprising its data source, and it will display those values. The only public property that the String class exposes is the Length property. You need to modify your Select clause a bit to provide better information. Adding in the creation of an anonymous type with the properties you are interested in will accomplish this:

                     From Elemento In xe.Elements("Line") _
                     Select New With
                         .ProductId = Elemento.Element("ProductId").Value,
                         .Description = Elemento.Element("Description").Value,
                         .Quantity = Elemento.Element("Quantity").Value,
                         .UnitPrice = Elemento.Element("UnitPrice").Value

Open in new window

You'll notice that I changed your From clause slightly. What you had will select each element under a <Line> node and treat them as individual nodes. I think what you really want is to treat them all properties of one entity. This is what my suggestion above is doing.


Author Closing Comment

ID: 39196730

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 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

22 Experts available now in Live!

Get 1:1 Help Now