dataset help, adding a column inside a loop

Discussion in 'ASP .Net' started by dSchwartz, Feb 27, 2004.

  1. dSchwartz

    dSchwartz Guest

    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>
    ========================================
     
    dSchwartz, Feb 27, 2004
    #1
    1. Advertising

  2. 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();
    > }
     
    Martin Dechev, Feb 27, 2004
    #2
    1. Advertising

  3. dSchwartz

    dSchwartz Guest

    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" <> wrote in message news:<uBFqh0W$>...
    > 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();
    > > }
     
    dSchwartz, Mar 1, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nedu N
    Replies:
    1
    Views:
    1,839
    Chris Taylor
    Oct 31, 2003
  2. Nedu N
    Replies:
    2
    Views:
    2,208
  3. Do
    Replies:
    2
    Views:
    6,180
    Natty Gur
    Nov 16, 2003
  4. Nedu N
    Replies:
    2
    Views:
    243
    Jacob Yang [MSFT]
    Oct 31, 2003
  5. Isaac Won
    Replies:
    9
    Views:
    457
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page