Solved

Show selected xml in gridview from linq query

Posted on 2013-05-25
2
338 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
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…

860 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