I am trying to take values from an XML file and put them in an array of strings. Here is the code I use for this:
public static string[] GetStringArray(string path)
{
var doc = XDocument.Load(path);
var services = from service in doc.Descendants("Service")
select (string)service.Attribute("name");
return services.ToArray();
}
But whenever I use it, I get a NullReferenceException:
foreach (string @string in query)
WeatherServicesCBO.Items.Add(@string);
From this method:
public void InitializeDropDown(string XmlFile, string xpath)
{
string[] services = GetStringArray("SupportedWeatherServices.xml");
IEnumerable<string> query = from service in services
orderby service.Substring(0, 1) ascending
select service;
foreach (string @string in query)
WeatherServicesCBO.Items.Add(@string);
}
EDIT The XML file is used here.
<?xml version="1.0" encoding="utf-8" ?>
<SupportedServices>
<Service>
<name>Google Weather</name>
<active>Yes</active>
</Service>
<Service>
<name>WeatherBug</name>
<active>No</active>
</Service>
<Service>
<name>Yahoo Weather</name>
<active>No</active>
</Service>
<Service>
<name>NOAA</name>
<active>No</active>
</Service>
</SupportedServices>
source
share