arrays - C# use Linq to pull data from xml -
i have problem building object holding xml data using linq. example, have xml data in url http://api.eve-central.com/api/marketstat?typeid=34&usesystem=30000142 . in marketstat
class want hold type id
value , in marketvalue
class array want hold volume
avg
max
min
stddev
median
percentile
values of buy
sell
all
nodes. have never used linq far please me fix problem in code below:
using system; using system.collections.generic; using system.linq; using system.xml.linq; namespace consoleapplication1 { internal class marketvalue { public int volume { get; set; } public double avg { get; set; } public double max { get; set; } public double min { get; set; } public double stddev { get; set; } public double median { get; set; } public double percentile { get; set; } } internal class marketstat { public string name { get; set; } public marketvalue[] marketvalueses { get; set; } } internal class program { private static list<marketstat> list; internal static void main(string[] args) { list = ( e in xdocument.load("http://api.eve-central.com/api/marketstat?typeid=34&usesystem=30000142"). root.elements("marketstat") select new marketstat { name = (string) e.element("type id"), marketvalueses = ( mv in e.elements("buy") select new marketvalue { volume = (int) mv.element("volume"), avg = (double) mv.element("avg"), max = (double)mv.element("max"), min = (double)mv.element("min"), stddev = (double)mv.element("stddev"), median = (double)mv.element("median"), percentile = (double)mv.element("percentile") }).toarray() }).tolist(); } } }
the problem current code want fetch id
attribute of type
element trying fetch type id
wrong. have values of marketvalue
in 3 nodes i.e. buy,sell & all fetching details buy node.
this should give expected output:-
xdocument xdoc = xdocument.load("http://api.eve-central.c... var result = xdoc.root.elements("type") .select(ms => new marketstat { name = (string)ms.attribute("id"), marketvalueses = ms.elements() .select(mv => new marketvalue { volume = (long)mv.element("volume"), avg = (double)mv.element("avg"), max = (double)mv.element("max"), min = (double)mv.element("min"), stddev = (double)mv.element("stddev"), median = (double)mv.element("median"), percentile = (double)mv.element("percentile") }).toarray() }).tolist();
Comments
Post a Comment