Solved

Show selected xml in gridview from linq query

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

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
Perfect!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

8 Experts available now in Live!

Get 1:1 Help Now