c# - Select node values and select the max value -
i select "highest" depth, width , height value detail tags each "temp" product.
here xml example:
<root> <temp> <code>1234567</code> <detail> <depth>12.7</depth> <width>1.27</width> <height>15.24</height> </detail> <detailconversion> <detail> <depth>34.925</depth> <width>30.48</width> <height>19.05</height> </detail> </detailconversion> <detailconversion> <detail> <depth>34.925</depth> <width>30.48</width> <height>19.05</height> </detail> </detailconversion> </temp> <temp> <code>1234567</code> <detail> <depth>12.7</depth> <width>1.27</width> <height>15.24</height> </detail> <detailconversion> <detail> <depth>34.925</depth> <width>30.48</width> <height>19.05</height> </detail> </detailconversion> <detailconversion> <detail> <depth>34.925</depth> <width>30.48</width> <height>19.05</height> </detail> </detailconversion> </temp> </root>
i have tryed
int maxdepth = doc.root.elements().max(x => (int)x.element("depth")); int maxwidth = doc.root.elements().max(x => (int)x.element("width")); int maxheight = doc.root.elements().max(x => (int)x.element("height"));
but have realy no iea why not works, maxdepth 0. maxdepth...must selcted each , not together
do have ideas ?
update complete xml
<?xml version="1.0" encoding="utf-8"?> <xmlinterchange xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns="http://www.edi.com.au/enterpriseservice/" version="1" > <interchangeinfo> <date>2015-05-29t14:17:45</date> </interchangeinfo> <payload> <temporaer> <temp> <tempcode>st66676eu</tempcode> <detailconversion> <depth>12.7</depth> <width>1.27</width> <height>15.24</height> </detailconversion> <detailconversions> <detailconversion> <depth>16.51</depth> <width>13.97</width> <height>6.35</height> </detailconversion> <detailconversion> <depth>34.925</depth> <width>30.48</width> <height>19.05</height> </detailconversion> </detailconversions> </temp> </temporaer> </payload> </xmlinterchange>
your codes return 0
because there no depth
/width
/height
direct child of <temp>
. max depth, max width, , max height each <temp>
element, can try way :
var result = doc.root .elements() .select(o => new { maxdepth = o.descendants("depth").max(x => (double)x), maxwidth = o.descendants("width").max(x => (double)x), maxheight = o.descendants("height").max(x => (double)x) }) .tolist();
result
variable list of anonymous objects each object carries information of maximum values of each temp
.
update :
your actual xml has default namespace. can use combination of xnamespace
+"element name"
select element in namespace :
xnamespace d = "http://www.edi.com.au/enterpriseservice/"; var result = doc.root .descendants(d+"temp") .select(o => new { maxdepth = o.descendants(d+"depth").max(x => (double?)x), maxwidth = o.descendants(d+"width").max(x => (double?)x), maxheight = o.descendants(d+"height").max(x => (double?)x) }) .tolist();
Comments
Post a Comment