public string[] UnpackXML(string xml_string)
{
string response = xml_string;
string[] return_values = new string[6];
XmlDocument xml = new XmlDocument();
xml.LoadXml(response);
return_values[0] = xml.GetElementsByTagName("meas name=\"mt1\"")[0].InnerText.ToString();
return_values[1] = xml.GetElementsByTagName("meas name=\"mt2\"")[0].InnerText.ToString();
return_values[2] = xml.GetElementsByTagName("meas name=\"mt3\"")[0].InnerText.ToString();
return_values[3] = xml.GetElementsByTagName("meas name=\"mt4\"")[0].InnerText.ToString();
return_values[4] = xml.GetElementsByTagName("meas name=\"mt5\"")[0].InnerText.ToString();
return_values[5] = xml.GetElementsByTagName("meas name=\"mt6\"")[0].InnerText.ToString();
return return_values;
}
When running the above code, it fails with the given error code:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
In the first line where I try to give return_values[0]
a new value:
return_values[0] = xml.GetElementsByTagName("meas name=\"mt1\"")[0].InnerText.ToString();
The input to the UnpackXML
is just an API response given as an XML string. The XML document has the following format:
<response location='location1'>
<meas name='mt1'>14</meas>
<meas name='mt2'>23</meas>
<meas name='mt3'>65</meas>
<meas name='mt4'>31</meas>
<meas name='mt6'>32</meas>
</response>
Any ideas on how to fix this? I basically want to append specific fields from the XML file to an array. The XML attribute "name" exists with each line and has different values for the attribute. Is there a way to directly access the attribute values by giving the correct name = "nameValue"
, without looping through attributes and checking each specific value?