KMBonin KMBonin - 21 days ago 8
C# Question

XML API Children Return Function?

I'm performing an API call to a controller that returns XML. The output is:

<ArrayOfreport_overallTime>
<report_overallTime>
<label>United States</label>
<value>65</value>
</report_overallTime>
<report_overallTime>
<label>United Kingdom</label>
<value>58</value>
</report_overallTime>
<report_overallTime>
<label>Germany</label>
<value>30</value>
</report_overallTime>
<report_overallTime>
<label>India</label>
<value>65</value>
</report_overallTime>
<report_overallTime>
<label>Russia</label>
<value>65</value>
</report_overallTime>
<report_overallTime>
<label>Germany</label>
<value>30</value>
</report_overallTime>
</ArrayOfreport_overallTime>


This is generated from the following C# controller code:

public class report_overallTimeController : ApiController
{
public void report_overallTime(HttpContext context)
{
var request = HttpContext.Current.Request;
var settings = Properties.Settings.Default;
GetAllItems();
}

report_overallTime[] items = new report_overallTime[]
{
new report_overallTime { label = "United States", value = "65" },
new report_overallTime { label = "United Kingdom", value = "58" },
new report_overallTime { label = "Germany", value = "30" },
new report_overallTime { label = "India", value = "65" },
new report_overallTime { label = "Russia", value = "65" },
new report_overallTime { label = "Germany", value = "30" }
};

public IEnumerable<report_overallTime> GetAllItems()
{
return items;
}
}


In my jQuery, I'm attempting to get the label/value pairs and insert them into an array.

function initChart(xml) {
var data = [];
//alert((new XMLSerializer()).serializeToString(xml));
$(xml).find('ArrayOfreport_overallTime report_overallTime').each(function (index) {
var label = $(this).children("label").text;
var value = $(this).children("value").text;
data.push([label, parseFloat(value)]);
});
//Do something with data...
}


When I try to output the values of var label, I get the following on my alert:

function(a) {
return Y(this, function(a) {
return void 0 === a ? n.text(this) : this.empty().append((this[0] && this[0].ownerDocument || d).createTextNode(a))
}, null, a, arguments.length)
}


What am I doing wrong? I am expecting to be alerted with the country.

Answer

The issue is because the ArrayOfreport_overallTime element is the root of the XML string, so you need to use filter() to retrieve that, then find() to get the report_overallTime. You can also use map() to build your array. Try this:

var xml = '<ArrayOfreport_overallTime><report_overallTime><label>United States</label><value>65</value></report_overallTime><report_overallTime><label>United Kingdom</label><value>58</value></report_overallTime><report_overallTime><label>Germany</label><value>30</value></report_overallTime><report_overallTime><label>India</label><value>65</value></report_overallTime><report_overallTime><label>Russia</label><value>65</value></report_overallTime><report_overallTime><label>Germany</label><value>30</value></report_overallTime></ArrayOfreport_overallTime>';

var data = $(xml).filter('ArrayOfreport_overallTime').find('report_overallTime').map(function() {
    var $el = $(this);
    return [[$el.children("label").text(), parseFloat($el.children("value").text())]];
}).get();

console.log(data);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Comments