Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Show selected xml in gridview from linq query

Posted on 2013-05-25
2
Medium Priority
?
342 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
[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
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

705 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