Solved

Get Data From XML?

Posted on 2009-05-19
1
183 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 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

729 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