public XElement saveTestParamsToFile(F28LightCtrl.F28_PARAMETERS testParam, string profileName) { XElement settingNode = new XElement("Settings"); XAttribute name = new XAttribute("name", profileName); settingNode.Add(name); foreach (string probName in paramNames) { XElement paramNode = new XElement("Parameter"); var field = testParam.GetType().GetField(probName); object val = field.GetValue(testParam); XAttribute at_name = new XAttribute("property_name", probName); paramNode.Add(at_name); XAttribute at_val = new XAttribute("value", val); paramNode.Add(at_val); XAttribute at_type; if (field.FieldType.FullName == "System.UInt16") { at_type = new XAttribute("type", "ushort"); } else { at_type = new XAttribute("type", "float"); } paramNode.Add(at_type); settingNode.Add(paramNode); } return(settingNode); }
public void loadParameterFromXML(string xmlFile) { xdoc = XDocument.Load(xmlFile); if (xdoc == null) { throw (new Exception("Unable to load XML file: " + xmlFile)); } var nodes = xdoc.Descendants("Settings"); foreach (XElement n in nodes) { string profileName = n.Attribute("name").Value; F28LightCtrl.F28_PARAMETERS tParams = new F28LightCtrl.F28_PARAMETERS(); var paramNode = n.Descendants("Parameter"); object tP = tParams; foreach (XElement el in paramNode) { string probName = el.Attribute("property_name").Value; string typeStr = el.Attribute("type").Value; string strVal = el.Attribute("value").Value; var field = tParams.GetType().GetField(probName); if (typeStr == "ushort") { ushort uValue = ushort.Parse(strVal); field.SetValue(tP, uValue); } else { float fValue = float.Parse(strVal); field.SetValue(tP, fValue); } } tParams = (F28LightCtrl.F28_PARAMETERS)tP; _controller.addParamToTable(profileName, tParams); } }