Let's say we have the following XML file which look likes
<model uri="model1">
<PriceData>
<value price="24.28" date="2013-12-01"/>
<value price="22.34" date="2013-12-02"/>
<value price="24.12" date="2013-12-03"/>
</PriceData>
</model>
<model uri="model2">
<PriceData>
<value price="24.28" date="2013-12-01"/>
<value price="22.34" date="2013-12-02"/>
<value price="24.12" date="2013-12-03"/>
</PriceData>
</model>
and this can continue for many models and prices. I search a way to parse in different lists (or array) the price of each model without do it manually i.e. to preallocate lists etc. because the number of the model will not be the same all the time. So how can i make "buckets" of prices for different models? What i tried until now is
using System;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Collections.Generic;
namespace test
{
class Program
{
static void Main(string[] args)
{
string file = @"C:\Users\user\Desktop\test.xml";
XDocument doc = XDocument.Load(file);
List<string> priceData = new List<string>();
List<string> modelName = new List<string>();
foreach (var imovel in doc.Root.Descendants("model"))
{
modelName.Add(imovel.Attribute("uri").Value);
}
int modelNum = modelName.Count();
foreach (var item in doc.Descendants("value"))
{
var doubleAttr = item.Attribute("price");
if (doubleAttr == null) continue;
priceData.Add(item.Attribute("price").Value);
}
//Console.WriteLine(modelName[0]);
Console.ReadKey();
}
}
}
But in this case the second foreach loop takes the prices of all models in a list. I try to get some ideas...my cup of coffee is empty :-(