Exemplo n.º 1
0
        public static void TextToXml(string inDirectory, string inFile)
        {
            List <string> channelNames = new List <string>();
            List <double> timeSlices   = new List <double>();
            List <Column> colDataSet   = new List <Column>();
            StreamReader  sReader      = new StreamReader(inDirectory + inFile);

            channelNames = GetChannelNames(sReader);

            colDataSet = InitializeColDataSet(channelNames.Count);


            #region Read File. Store data in columns.

            int rowIndex = 0;
            int totalRows;
            while (!sReader.EndOfStream)
            {
                List <string> rowValues = ReadRows(sReader, timeSlices);

                if (rowValues == null)
                {
                    continue;
                }

                colDataSet = RowToColumn(colDataSet, channelNames.Count, rowIndex, rowValues);

                rowIndex++;
            }

            totalRows = rowIndex;
            #endregion


            #region Write Xml Data


            XmlDocument doc         = new XmlDocument();
            XmlNode     declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);

            XmlNode      fileNameNode      = doc.CreateElement("File_Name");
            XmlAttribute fileNameAttribute = doc.CreateAttribute("value");
            fileNameAttribute.Value = inFile;
            fileNameNode.Attributes.Append(fileNameAttribute);
            doc.AppendChild(fileNameNode);


            int channelIndex = 0;
            int rowDeficit   = 10 - (rowIndex % 10);

            foreach (Column column in colDataSet)
            {
                int         timeSliceIndex = 0;
                StimulusSet stimulusSet    = new StimulusSet();
                stimulusSet.index = 0;
                stimulusSet.size  = 0;

                // channel node
                XmlNode      channelNode      = doc.CreateElement("Channel");
                XmlAttribute channelAttribute = doc.CreateAttribute("id");

                // We only want the number preceeding the "-Min[uV]" portion of the name
                // We will end up with channel id's like "21" , "52" , etc.
                int    indexOfDash = channelNames[channelIndex].IndexOf("-");
                string channelName = channelNames[channelIndex].Substring(0, indexOfDash);


                channelAttribute.Value = channelName;
                channelNode.Attributes.Append(channelAttribute);
                fileNameNode.AppendChild(channelNode);

                /**
                 * For each column, we clump every 10 elements together in what is called
                 * a stimulus set. Each stimulus set consists of elements with increasing
                 * currents. After a set, the current is reset, and 10 more tests are run.
                 **/
                XmlNode      stimulusSetNode      = doc.CreateElement("StimulusSet");
                XmlAttribute stimulusSetAttribute = doc.CreateAttribute("id");
                stimulusSetNode.Attributes.Append(stimulusSetAttribute);

                foreach (DataElement element in column.colElements)
                {
                    // In other words, are we looking at the very first element?
                    // If so, create a new XML "element"
                    if (stimulusSet.index == 0 && stimulusSet.size == 0)
                    {
                        stimulusSetNode            = doc.CreateElement("StimulusSet");
                        stimulusSetAttribute       = doc.CreateAttribute("id");
                        stimulusSetAttribute.Value = stimulusSet.index.ToString();

                        stimulusSetNode.Attributes.Append(stimulusSetAttribute);
                        channelNode.AppendChild(stimulusSetNode);
                    }

                    /*
                     * Are we looking at what should be the last element of the first stimulus set?
                     * This is determined by the row deficit
                     *
                     **/
                    if (stimulusSet.index == 0 && ((stimulusSet.size == 10 - rowDeficit)))
                    {
                        stimulusSet.index++;
                        stimulusSet.size           = 0;
                        stimulusSetNode            = doc.CreateElement("StimulusSet");
                        stimulusSetAttribute       = doc.CreateAttribute("id");
                        stimulusSetAttribute.Value = stimulusSet.index.ToString();

                        stimulusSetNode.Attributes.Append(stimulusSetAttribute);
                        channelNode.AppendChild(stimulusSetNode);
                    }


                    /*
                     * If this is any set other than the first, it's size should be 10.
                     *
                     **/
                    else if (stimulusSet.size == 10)
                    {
                        stimulusSet.index++;
                        stimulusSet.size           = 0;
                        stimulusSetNode            = doc.CreateElement("StimulusSet");
                        stimulusSetAttribute       = doc.CreateAttribute("id");
                        stimulusSetAttribute.Value = stimulusSet.index.ToString();

                        stimulusSetNode.Attributes.Append(stimulusSetAttribute);
                        channelNode.AppendChild(stimulusSetNode);
                    }

                    XmlNode      timeSliceNode           = doc.CreateElement("Time");
                    XmlAttribute timeSliceAttributeIndex = doc.CreateAttribute("index");
                    XmlAttribute timeSliceAttributeTime  = doc.CreateAttribute("time");

                    // We want to shift the time slice index to account for missing rows
                    // This way we can safely match up indices within each stimulus set in
                    // order to take averages and deal with the data in general

                    timeSliceAttributeIndex.Value = (timeSliceIndex + rowDeficit).ToString();
                    timeSliceAttributeTime.Value  = timeSlices[timeSliceIndex].ToString();
                    timeSliceNode.Attributes.Append(timeSliceAttributeIndex);
                    timeSliceNode.Attributes.Append(timeSliceAttributeTime);


                    timeSliceNode.AppendChild(doc.CreateTextNode(element.value.ToString()));
                    stimulusSetNode.AppendChild(timeSliceNode);

                    timeSliceIndex++;
                    stimulusSet.size++;
                }

                channelIndex++;
            }

            string outFileName = inFile.Substring(0, inFile.LastIndexOf("."));
            //doc.Save(directory + outFileName + ".xml");
            using (TextWriter sw = new StreamWriter(inDirectory + outFileName + ".xml", false, Encoding.UTF8)) //Set encoding
            {
                doc.Save(sw);
            }

            #endregion
        }
Exemplo n.º 2
0
        public static void TextToXml(string inDirectory, string inFile)
        {
            List<string> channelNames = new List<string>();
            List<double> timeSlices = new List<double>();
            List<Column> colDataSet = new List<Column>();
            StreamReader sReader = new StreamReader(inDirectory + inFile);

            channelNames = GetChannelNames(sReader);

            colDataSet = InitializeColDataSet(channelNames.Count);

            #region Read File. Store data in columns.

            int rowIndex = 0;
            int totalRows;
            while (!sReader.EndOfStream)
            {
                List<string> rowValues = ReadRows(sReader, timeSlices);

                if (rowValues == null)
                    continue;

                colDataSet = RowToColumn(colDataSet, channelNames.Count, rowIndex, rowValues);

                rowIndex++;
             }

             totalRows = rowIndex;
             #endregion

            #region Write Xml Data

            XmlDocument doc = new XmlDocument();
            XmlNode declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);

            XmlNode fileNameNode = doc.CreateElement("File_Name");
            XmlAttribute fileNameAttribute = doc.CreateAttribute("value");
            fileNameAttribute.Value = inFile;
            fileNameNode.Attributes.Append(fileNameAttribute);
            doc.AppendChild(fileNameNode);

            int channelIndex = 0;
            int rowDeficit = 10 - (rowIndex % 10);

            foreach (Column column in colDataSet)
            {
                int timeSliceIndex = 0;
                StimulusSet stimulusSet = new StimulusSet();
                stimulusSet.index = 0;
                stimulusSet.size = 0;

                // channel node
                XmlNode channelNode = doc.CreateElement("Channel");
                XmlAttribute channelAttribute = doc.CreateAttribute("id");

                // We only want the number preceeding the "-Min[uV]" portion of the name
                // We will end up with channel id's like "21" , "52" , etc.
                int indexOfDash = channelNames[channelIndex].IndexOf("-");
                string channelName = channelNames[channelIndex].Substring(0, indexOfDash);

                channelAttribute.Value = channelName;
                channelNode.Attributes.Append(channelAttribute);
                fileNameNode.AppendChild(channelNode);

                /**
                 * For each column, we clump every 10 elements together in what is called
                 * a stimulus set. Each stimulus set consists of elements with increasing
                 * currents. After a set, the current is reset, and 10 more tests are run.
                **/
                XmlNode stimulusSetNode = doc.CreateElement("StimulusSet");
                XmlAttribute stimulusSetAttribute = doc.CreateAttribute("id");
                stimulusSetNode.Attributes.Append(stimulusSetAttribute);

                foreach (DataElement element in column.colElements)
                {

                    // In other words, are we looking at the very first element?
                    // If so, create a new XML "element"
                    if (stimulusSet.index == 0 && stimulusSet.size == 0)
                    {
                        stimulusSetNode = doc.CreateElement("StimulusSet");
                        stimulusSetAttribute = doc.CreateAttribute("id");
                        stimulusSetAttribute.Value = stimulusSet.index.ToString();

                        stimulusSetNode.Attributes.Append(stimulusSetAttribute);
                        channelNode.AppendChild(stimulusSetNode);

                    }

                    /*
                     * Are we looking at what should be the last element of the first stimulus set?
                     * This is determined by the row deficit
                     *
                     **/
                    if (stimulusSet.index == 0 && ((stimulusSet.size == 10 - rowDeficit)))
                    {

                        stimulusSet.index++;
                        stimulusSet.size = 0;
                        stimulusSetNode = doc.CreateElement("StimulusSet");
                        stimulusSetAttribute = doc.CreateAttribute("id");
                        stimulusSetAttribute.Value = stimulusSet.index.ToString();

                        stimulusSetNode.Attributes.Append(stimulusSetAttribute);
                        channelNode.AppendChild(stimulusSetNode);

                    }

                    /*
                     * If this is any set other than the first, it's size should be 10.
                     *
                     **/
                    else if (stimulusSet.size == 10)
                    {
                        stimulusSet.index++;
                        stimulusSet.size = 0;
                        stimulusSetNode = doc.CreateElement("StimulusSet");
                        stimulusSetAttribute = doc.CreateAttribute("id");
                        stimulusSetAttribute.Value = stimulusSet.index.ToString();

                        stimulusSetNode.Attributes.Append(stimulusSetAttribute);
                        channelNode.AppendChild(stimulusSetNode);

                    }

                    XmlNode timeSliceNode = doc.CreateElement("Time");
                    XmlAttribute timeSliceAttributeIndex = doc.CreateAttribute("index");
                    XmlAttribute timeSliceAttributeTime = doc.CreateAttribute("time");

                    // We want to shift the time slice index to account for missing rows
                    // This way we can safely match up indices within each stimulus set in
                    // order to take averages and deal with the data in general

                    timeSliceAttributeIndex.Value = (timeSliceIndex + rowDeficit).ToString();
                    timeSliceAttributeTime.Value = timeSlices[timeSliceIndex].ToString();
                    timeSliceNode.Attributes.Append(timeSliceAttributeIndex);
                    timeSliceNode.Attributes.Append(timeSliceAttributeTime);

                    timeSliceNode.AppendChild(doc.CreateTextNode(element.value.ToString()));
                    stimulusSetNode.AppendChild(timeSliceNode);

                    timeSliceIndex++;
                    stimulusSet.size++;
                }

                channelIndex++;
            }

            string outFileName = inFile.Substring(0, inFile.LastIndexOf("."));
            //doc.Save(directory + outFileName + ".xml");
            using (TextWriter sw = new StreamWriter(inDirectory + outFileName + ".xml", false, Encoding.UTF8)) //Set encoding
            {
                doc.Save(sw);
            }

            #endregion
        }