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

x
?
Solved

Get Data From XML?

Posted on 2009-05-19
1
Medium Priority
?
186 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
[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
1 Comment
 
LVL 9

Accepted Solution

by:
tetorvik earned 2000 total points
ID: 24420105
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

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