jasonsalas
IS-IT--Management
Hi everyone,
I'm trying to develop a control that will allow a page developer to use their own XML documents to populate a page's contents. As such, I'm trying to have the size and contents of an array populated dynamically, as opposed to hard-coded.
In the GetArrayContents method below, I'm trying to read the DataColumns from a DataTable and write a string value to be read by the ReadSlides method to populate a jagged array, but I keep getting a "Index Outside the Bounds of the Array" error. I thought this would be OK, but apparently not.
// set a jagged array of images & captions to use by reading data from an XML file
private string[][] ReadSlides(string filepath)
{
FileStream fs = new FileStream(filepath,FileMode.Open,FileAccess.Read);
StreamReader sr = new StreamReader(fs);
DataSet ds = new DataSet();
ds.ReadXml(sr);
fs.Close();
DataTable table = ds.Tables[0];
DataRow[] rows = table.Select();
string[][] contents = new string[rows.Length][];
for(int i=0;i<rows.Length;i++)
{
// contents = new string[] { rows["image"].ToString(),rows["alt"].ToString(),rows["title"].ToString(),rows["caption"].ToString() };
contents = new string[] { GetArrayContents(table) };
}
return contents;
}
// helper method that automates the population of the jagged array
private string GetArrayContents(DataTable table)
{
string arrayContents = string.Empty;
foreach(DataColumn col in table.Columns)
{
arrayContents += "rows[\"" + col.ColumnName + "\"].ToString(),";
}
return arrayContents;
}
Thanks!
I'm trying to develop a control that will allow a page developer to use their own XML documents to populate a page's contents. As such, I'm trying to have the size and contents of an array populated dynamically, as opposed to hard-coded.
In the GetArrayContents method below, I'm trying to read the DataColumns from a DataTable and write a string value to be read by the ReadSlides method to populate a jagged array, but I keep getting a "Index Outside the Bounds of the Array" error. I thought this would be OK, but apparently not.
// set a jagged array of images & captions to use by reading data from an XML file
private string[][] ReadSlides(string filepath)
{
FileStream fs = new FileStream(filepath,FileMode.Open,FileAccess.Read);
StreamReader sr = new StreamReader(fs);
DataSet ds = new DataSet();
ds.ReadXml(sr);
fs.Close();
DataTable table = ds.Tables[0];
DataRow[] rows = table.Select();
string[][] contents = new string[rows.Length][];
for(int i=0;i<rows.Length;i++)
{
// contents = new string[] { rows["image"].ToString(),rows["alt"].ToString(),rows["title"].ToString(),rows["caption"].ToString() };
contents = new string[] { GetArrayContents(table) };
}
return contents;
}
// helper method that automates the population of the jagged array
private string GetArrayContents(DataTable table)
{
string arrayContents = string.Empty;
foreach(DataColumn col in table.Columns)
{
arrayContents += "rows[\"" + col.ColumnName + "\"].ToString(),";
}
return arrayContents;
}
Thanks!