473,320 Members | 1,991 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

dataset help, adding a column inside a loop

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
2 2011
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alex Ayzin | last post by:
Hi, I have a problem that might be easy to solve(possibly, I've just overlooked an easy solution). Here we go: I have a dataset with 2 datatables in it. Now, I need to do the following: if...
10
by: Eric Petruzzelli | last post by:
If I fill my dataset and there is no data. The dataset is still created with zero rows (all columns are there). When I add my first row using the script below, it takes over 2 seconds to add??? If...
1
by: Piyush Gupta | last post by:
Hi All, I am using following code snippet to add another user in Users.xml: ------------------------------------------------------------------- DataSet dstUsers = new DataSet();...
3
by: jason | last post by:
I'll try to explain in detail what it is I'm struggling with. I created a SQL database with one table called CallSheet which contains the following columns: Caller ID: <--Primary Key, auto...
16
by: Geoff Jones | last post by:
Hi Can anybody help me with the following, hopefully simple, question? I have a table which I've connected to a dataset. I wish to add a new column to the beginning of the table and to fill...
10
by: Bernie Yaeger | last post by:
I have a need to add a primary key to a dataset/datatable. How can this be done using a standard oledb data provider? Tx for any help.
4
by: Robert Bravery | last post by:
Hi all, I have now correctly set up my dataset and two grids, so that the parent navigates the child. THe thing is that the child table is actually a lookup type table. It lists billing types for...
2
by: tshad | last post by:
Is there a way to update a DataSet Object? I have a Dataset Object (DataSetObj.Tables(0) - one table) that I read in from a .csv file. I normally do the following to get my data from the .csv:...
3
by: Ken Fine | last post by:
This is a question that someone familiar with ASP.NET and ADO.NET DataSets and DataTables should be able to answer fairly easily. The basic question is how I can efficiently match data from one...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.