dataset help, adding a column inside a loop

D

dSchwartz

I need help adding a column to a dataset, but its a little bit more
complicated then just that. Here's the situation: I have many xml
files in one directory, each which represent a newsletter. I want to
create a page that will display a title and date of each newsletter,
along with a link to it. here's a code snip:

=============================

DirectoryInfo dirInfo = new
DirectoryInfo(Server.MapPath("/newsletters/xml/"));
FileInfo[] newsletters = dirInfo.GetFiles("*.xml");
DataSet objDataSet = new DataSet();
string strVirtualPath = "";
foreach (FileInfo xfile in newsletters)
{ strVirtualPath = "/smartmail/xml/" + xfile;
objDataSet.ReadXml(Request.MapPath(strVirtualPath));
articleList.DataSource = objDataSet.Tables;
articleList.DataBind();

DataView objDataView = new DataView(objDataSet.Tables[0]);
objDataView.Sort = "title";

articleValues.DataSource = objDataView;
articleValues.DataBind();
}

=================================

this produces a table (bound to datagrid) with one row for each xml
file. the columns are newsletter_ID, title, and date, which is good,
but i want to add a column that will contain the actual filename of
the xml file. How could this be accomplished?

Thanks for your time!


I don't think it will be needed to help me, but just in case, here is
a sample of the xml files:
=======================================
<?xml version="1.0" encoding="UTF-8"?>

<newsletter title="newsletter title" date="28 Feb 2004">
<article>
<title>specific article title</title>
<author>DJS</author>
<date>Jan 6 2004</date>
<body>specific article text here.</body>
</article>
<article>
.....
</article>
</newsletter>
========================================
 
M

Martin Dechev

Try the following:
DirectoryInfo dirInfo = new
DirectoryInfo(Server.MapPath("/newsletters/xml/"));
FileInfo[] newsletters = dirInfo.GetFiles("*.xml");
DataSet objDataSet = new DataSet();
string strVirtualPath = "";
foreach (FileInfo xfile in newsletters)
{ strVirtualPath = "/smartmail/xml/" + xfile;
objDataSet.ReadXml(Request.MapPath(strVirtualPath));

objDataSet.Tables[0].Columns.Add(
new System.Data.DataColumn("File Name", typeof(string)));
//it should have only one row, so we don't need to loop
//foreach(DataRow dr in objDataSet.Tables[0].Rows)
objDataSet.Tables[0].Rows[0][dr.Columns.Count - 1] = xfile;
articleList.DataSource = objDataSet.Tables;
articleList.DataBind();

DataView objDataView = new DataView(objDataSet.Tables[0]);
objDataView.Sort = "title";

articleValues.DataSource = objDataView;
articleValues.DataBind();
}
 
D

dSchwartz

Thanks Martin,
I tried this out and I get an error:
"A Column named 'name_of_file' already belongs to this DataTable."

I figured that this was because the Columns.Add line is inside a loop
so I tried to hard code only one xml file to use and it worked fine
(got rid of the loop). I tried to move the Columns.Add line outside
of the loop but I think it needs to be below the ReadXml line?

Maybe someone could point out an easier way to loop through several
xml files in a directory and populating a dataset with 2 elements from
each file along with the filename?

Thanks for your time!



Martin Dechev said:
Try the following:
DirectoryInfo dirInfo = new
DirectoryInfo(Server.MapPath("/newsletters/xml/"));
FileInfo[] newsletters = dirInfo.GetFiles("*.xml");
DataSet objDataSet = new DataSet();
string strVirtualPath = "";
foreach (FileInfo xfile in newsletters)
{ strVirtualPath = "/smartmail/xml/" + xfile;
objDataSet.ReadXml(Request.MapPath(strVirtualPath));

objDataSet.Tables[0].Columns.Add(
new System.Data.DataColumn("File Name", typeof(string)));
//it should have only one row, so we don't need to loop
//foreach(DataRow dr in objDataSet.Tables[0].Rows)
objDataSet.Tables[0].Rows[0][dr.Columns.Count - 1] = xfile;
articleList.DataSource = objDataSet.Tables;
articleList.DataBind();

DataView objDataView = new DataView(objDataSet.Tables[0]);
objDataView.Sort = "title";

articleValues.DataSource = objDataView;
articleValues.DataBind();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top