Solved

Get Data From XML?

Posted on 2009-05-19
1
181 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
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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 aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 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