By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,844 Members | 1,889 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,844 IT Pros & Developers. It's quick & easy.

dataset help, adding a column inside a loop

P: n/a
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>
========================================
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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();
}

Nov 18 '05 #2

P: n/a
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" <de******@hotmail.com> wrote in message news:<uB**************@TK2MSFTNGP11.phx.gbl>...
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();
}

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.