Solved

Xml Get subelements to Datagridview

Posted on 2013-05-23
7
219 Views
Last Modified: 2013-05-25
I have this code but only return the first line of each element InvoiceNum. For ex. Invoicenum 154 has two lines, but i can get only one line, the first. How can get all lines from each invoice.

<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

filepath1 = "C:\Users\Documents\WorkP\March2013.xml"
       Dim XmlDoc As XDocument = RemoveXmlNamespace(XDocument.Load(filepath1))
        Dim query = From xe In XmlDoc.Descendants("Invoice")
              Where xe.Element("InvoiceNo").Value = "154"
              Select New With {
                 .Factura = xe.Element("InvoiceNo").Value,
                 .Cliente = xe.Element("CustomerID").Value,
                 .Produto = xe.Element("Line").Element("ProductCode").Value
              }
        DataGridView1.DataSource = query.ToArray

Open in new window

0
Comment
Question by:rflorencio
  • 5
  • 2
7 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39190578
here:
filepath1 = "C:\Users\Documents\WorkP\March2013.xml"
       Dim XmlDoc As XDocument = RemoveXmlNamespace(XDocument.Load(filepath1))
        Dim query = From xe In XmlDoc.Descendants("Invoice")
              Where xe.Element("InvoiceNo").Value = "154"
              Select New With {
                 .Factura = xe.Element("InvoiceNo").Value,
                 .Cliente = xe.Element("CustomerID").Value,
                 .Produto = xe.Element("Line").Elements("ProductCode").Select(n=>n.Value)
              }
        DataGridView1.DataSource = query.ToArray

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39190581
look at line 8, instead of :
.Produto = xe.Element("Line").Element("ProductCode").Value

Open in new window

i used
.Produto = xe.Element("Line").Elements("ProductCode").Select(n=>n.Value)

Open in new window

so it returns for each line in the invoicenum, the ProductCode value.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39190586
now that i look at the xml there's no ProductCode element inside Line element.
did u mean u have ProductId?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:rflorencio
ID: 39191053
I have two xml files one with ProductCode and another with ProductId. I'm a little confused (with Linq and Collections), what should put where is "n"  in the code ".Select(n=>n.Value) ".
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39191137
I made a mistake, it should be:
 
.Produto = xe.Elements("Line").Select(n=>n.Element("ProductCode").Value)

Open in new window

this little linq code means loop through line elements and for each get productcode value.
The reason u got only the 1st one is cause u called xe.Element("Line") and not xe.Elements("Line").
xe.Element("Line") return the first one if exists.
0
 

Author Comment

by:rflorencio
ID: 39191242
Sorry. Something is wrong with me. With the code provided ".Produto = xe.Elements("Line").Select(n=>n.Element("ProductCode").Value)", an error occurs saying "n is not declared".
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39191297
Sorry its c# i need to convert it to vb.net
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

12 Experts available now in Live!

Get 1:1 Help Now