Solved

Show selected xml in gridview from linq query

Posted on 2013-05-25
2
337 Views
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.

<Documents>
	   <Sales>
		<Total>81678.600000</Total>
		<Invoice>
			<InvoiceNum>153</InvoiceNum>
			<Date>2013-03-01</Date>
			<Customer>2589</Customer>
			<Line>
				<ProductId>AT.02-2584</ProductId>
				<Description>Product 1</Description>
				<Quantity>1.000</Quantity>
				<UnitPrice>265.140000</UnitPrice>
			</Line>
		</Invoice>		
		<Invoice>
			<InvoiceNum>154</InvoiceNum>
			<Date>2013-03-01</Date>
			<Customer>2589</Customer>
			<Line>
				<ProductId>AT.02-2584</ProductId>
				<Description>Product 1</Description>
				<Quantity>1.000</Quantity>
				<UnitPrice>265.140000</UnitPrice>
			</Line>
                        <Line>
				<ProductId>AT.02-2599</ProductId>
				<Description>Product 2</Description>
				<Quantity>1.000</Quantity>
				<UnitPrice>85.100000</UnitPrice>
			</Line>
		</Invoice>	
	</Sales>
</Documents>

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
                MsgBox(Elemento)
            Next
        End If

Open in new window

0
Comment
Question by:rflorencio
2 Comments
 
LVL 75

Accepted Solution

by:
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
                     }).ToList

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.

Screenshot
0
 

Author Closing Comment

by:rflorencio
ID: 39196730
Perfect!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Multiple file Upload asp.net 2 49
Sending receiving text messages in vb.net 15 33
No Data for DropDown List 2 25
VB.NET 2008 Winforms Signing 13 18
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…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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…

772 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