Solved

Get Data From XML?

Posted on 2009-05-19
1
178 Views
Last Modified: 2012-05-07
I have an XML document which has a list of transactions of costs for different items, so it will be listed like this:
                  <product id="1" description="XYZ">
                        <costs>
                              <cost start="01/02/2009" cost="8.67000"/>
                              <cost start="01/03/2009" cost="6.80000"/>
                        </costs>
                  </product>
                  <product id="2" description="ABC">
                        <costs>
                              <cost start="01/02/2009" cost="8.67000"/>
                        </costs>
                  </product>

In my code I'm iterating through the XML getting each of the cost attributes, unfortunately my code doesn't differentiate between product 1 and 2, so adds all 6 attributes for cost to the one list.  

Is there anyway I can avoid grouping all of them together?  And take out X amount of costs from product 1, and X from product 2?  I can't go off the product ID as that will be different each time, and may not even be present.  All help is greatly appreciated.
0
Comment
Question by:angus_young_acdc
1 Comment
 
LVL 9

Accepted Solution

by:
tetorvik earned 500 total points
Comment Utility
with XPath you can first retrieve the list of products and then calculate the total cost/product in inner loop. Here is the sample:
XmlDocument doc = new XmlDocument();

doc.Load(Server.MapPath("XMLFile1.xml"));
 

foreach (XmlNode product in doc.SelectNodes("//product"))

{

    XmlNodeList costNodes = product.SelectNodes("costs/cost");

    //then loop throught the costNodes to retrieve total cost per product

    //foreach(XmlNode cost in costNodes) etc. 

}

Open in new window

0

Featured Post

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

Join & Write a Comment

Suggested Solutions

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now