public virtual string[][] getAllRecordsAs2DArray(string filePath, bool ignoreHeaders)
 {
     string[][] data = null;
     try
     {
         List <string>[] inputData = (new CSVDataManipulator()).getAllRecordsAsArrayList(filePath);
         int             cols      = inputData.Length;
         int             rows      = inputData[0].Count;
         data = RectangularArrays.ReturnRectangularStringArray(rows, cols);
         int i;
         if (ignoreHeaders)
         {
             i = 1;
         }
         else
         {
             i = 0;
         }
         for (; i < rows; i++)
         {
             for (int j = 0; j < cols; j++)
             {
                 data[i][j] = inputData[j][i];
                 Console.Write(data[i][j] + "|");
             }
             Console.WriteLine();
         }
         return(data);
     }
     catch (Exception e)
     {
         Console.WriteLine(e.ToString());
         Console.Write(e.StackTrace);
         return(data);
     }
 }
Beispiel #2
0
        /// <summary>
        /// Returns a double dimentional array with element values of an XML file
        /// passed as String Parameter. The sample file assumed to be in the below
        /// format. <?xml version="1.0"?> <Class> <Student id="1">
        /// <firstname>Kiran</firstname> <lastname>Kumar</lastname>
        /// <salary>10000</salary> </Student> <Student id="2">
        /// <firstname>Ram</firstname> <lastname>Rahim</lastname>
        /// <salary>20000</salary> </Student> </Class>
        /// </summary>
        /// <returns> String[][] is a 2D array with tabular representation of data. </returns>
        public virtual string[][] getXMLDataIn2DArray(string filePath)
        {
            string[][] data = null;
            int        rows = 0, cols = 0, p = 0, q = 0;

            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(filePath);
                doc.DocumentElement.Normalize();
                XmlNodeList allParentNodes = doc.DocumentElement.ChildNodes;
                XmlNodeList allChildNodes  = allParentNodes.Item(1).ChildNodes; // item(0)
                // is
                // a
                // text
                // node.
                // and
                // item(1)
                // is
                // an
                // element
                // node
                // To find number of records which are strictly element nodes.
                for (int i = 0; i < allParentNodes.Count; i++)
                {
                    if (allParentNodes.Item(i).NodeType == XmlNodeType.Element)
                    {
                        rows++;
                    }
                }
                // To find number of columns which are strictly element nodes.
                for (int j = 0; j < allChildNodes.Count; j++)
                {
                    if (allChildNodes.Item(j).NodeType == XmlNodeType.Element)
                    {
                        cols++;
                    }
                }
                data = RectangularArrays.ReturnRectangularStringArray(rows, cols);
                for (int i = 0; i < allParentNodes.Count; i++)
                {
                    if (allParentNodes.Item(i).NodeType == XmlNodeType.Element)
                    {
                        // System.out.println(allNodes.item(i).getNodeName());
                        allChildNodes = allParentNodes.Item(i).ChildNodes;

                        for (int j = 0; j < allChildNodes.Count; j++)
                        {
                            if (allChildNodes.Item(j).NodeType == XmlNodeType.Element)
                            {
                                string item = allChildNodes.Item(j).InnerText;
                                //string item = allChildNodes.Item(j).ToString();
                                // System.out.println(item);
                                data[p][q] = item;
                                // System.out.println("\t"+allChildNodes.item(j).getTextContent());
                                q++;
                            }
                        }
                        p++;
                        q = 0;
                        // We use p,q instead of i,j to store skip the non-element
                        // node indexes in the array.
                    }
                }
                return(data);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                Console.Write(e.StackTrace);
                return(data);
            }
        }