Example #1
0
        /// <summary>
        /// Reads a data object from XML and returns an instance of the object.
        /// </summary>
        /// <param name="reader">An open XML reader. The reader will be closed by this
        /// method after reading.</param>
        /// <returns>Returns <b>true</b> if successful; <b>false</b> otherwise.</returns>
        /// <remarks>Clients should first create a new instance using a default constructor, and then
        /// call this method to populate the data fields of the default instance.</remarks>
        public bool ReadFromXml(XmlTextReader reader)
        {
            reader.Read(); // <Condition>
            if (reader.Name != "Condition")
            {
                throw new XmlException("XML format error: Expected the <Condition> tag.");
            }

            _block    = XmlConvert.ToInt32(reader.GetAttribute("block"));
            _index    = XmlConvert.ToInt32(reader.GetAttribute("index"));
            _circular = XmlConvert.ToBoolean(reader.GetAttribute("circular"));
            int trials = XmlConvert.ToInt32(reader.GetAttribute("trials"));

            _mtpct  = XmlConvert.ToDouble(reader.GetAttribute("MTPct"));
            _mtpred = XmlConvert.ToInt64(reader.GetAttribute("MTPred"));
            _a      = XmlConvert.ToInt32(reader.GetAttribute("A"));
            _w      = XmlConvert.ToInt32(reader.GetAttribute("W"));

            // read in the trials and add them to the condition
            _trials = new List <TrialData>(trials + 1); // include slot for special start-area trial
            for (int i = 1; i <= trials; i++)
            {
                TrialData td;
                if (_circular)
                {
                    td = new TrialData2D();
                    if (!td.ReadFromXml(reader))
                    {
                        throw new XmlException("Failed to read the TrialData2D.");
                    }
                    if (i == 1)
                    {
                        TrialData2D sa = TrialData2D.CreateStartArea((TrialData2D)td);
                        _trials.Add(sa); // add special start-area trial
                    }
                }
                else // 1D
                {
                    td = new TrialData1D();
                    if (!td.ReadFromXml(reader))
                    {
                        throw new XmlException("Failed to read the TrialData1D.");
                    }
                    if (i == 1)
                    {
                        TrialData1D sa = TrialData1D.CreateStartArea((TrialData1D)td);
                        _trials.Add(sa); // add special start-area trial
                    }
                }

                // now add the trial just read in
                _trials.Add(td);
            }

            reader.Read(); // </Condition>
            if (reader.Name != "Condition" || reader.NodeType != XmlNodeType.EndElement)
            {
                throw new XmlException("XML format error: Expected the </Condition> tag.");
            }

            return(true);
        }