fm250
asked on
C# dynamic table cell/rows
what is wron with my table rows/cell. it only display the last var assigined which "summary" from the xml. note that looping through xml is correct.
I know I am not looping correctly. thanks!
I know I am not looping correctly. thanks!
Table tbl = new Table();
tbl.ID = "table1";
tbl.BorderWidth = 1;
this.Controls.Add(tbl);
var xmlDoc = XDocument.Load(new XmlTextReader(Server.MapPath("NewsSrc.xml")));
foreach (var descendant in xmlDoc.Descendants("NewsItem"))
{
TableRow rw = new TableRow();
TableCell cell = new TableCell();
var title = descendant.Element("Title").Value;
var summary = descendant.Element("Summary").Value;
var details = descendant.Element("Details").Value;
cell.Text = title;
rw.Cells.Add(cell);
tbl.Controls.Add(rw);
cell.Text = summary;
rw.Cells.Add(cell);
tbl.Controls.Add(rw);
}
--------------xml -----
<NewsItem id="1">
<Title> news tile one </Title>
<Summary> this is summary to show ....</Summary>
<Details>details for this news</Details>
</NewsItem>
<NewsItem id="2">
<Title>test title </Title>
<Summary>sm line</Summary>
<Details> no details</Details>
</NewsItem>
change the for loop code like below
foreach (var descendant in xmlDoc.Descendants("NewsIt em"))
{
TableRow rw = new TableRow();
TableCell cell = new TableCell();
var title = descendant.Element("Title" ).Value;
var summary = descendant.Element("Summar y").Value;
var details = descendant.Element("Detail s").Value;
cell.Text = title;
rw.Cells.Add(cell);
cell.Text = summary;
rw.Cells.Add(cell);
tbl.Controls.Add(rw);
}
you only need to add the row variable rw to table tbl variable once.
no need to add it more times
foreach (var descendant in xmlDoc.Descendants("NewsIt
{
TableRow rw = new TableRow();
TableCell cell = new TableCell();
var title = descendant.Element("Title"
var summary = descendant.Element("Summar
var details = descendant.Element("Detail
cell.Text = title;
rw.Cells.Add(cell);
cell.Text = summary;
rw.Cells.Add(cell);
tbl.Controls.Add(rw);
}
you only need to add the row variable rw to table tbl variable once.
no need to add it more times
there is another mistake
you declaring the cell only once
change the for loop code like below
foreach (var descendant in xmlDoc.Descendants("NewsIt em"))
{
TableRow rw = new TableRow();
TableCell cell = new TableCell();
var title = descendant.Element("Title" ).Value;
var summary = descendant.Element("Summar y").Value;
var details = descendant.Element("Detail s").Value;
cell.Text = title;
rw.Cells.Add(cell);
cell = new TableCell();
cell.Text = summary;
rw.Cells.Add(cell);
tbl.Controls.Add(rw);
}
you need to reinitialize the cell variable for each field
you declaring the cell only once
change the for loop code like below
foreach (var descendant in xmlDoc.Descendants("NewsIt
{
TableRow rw = new TableRow();
TableCell cell = new TableCell();
var title = descendant.Element("Title"
var summary = descendant.Element("Summar
var details = descendant.Element("Detail
cell.Text = title;
rw.Cells.Add(cell);
cell = new TableCell();
cell.Text = summary;
rw.Cells.Add(cell);
tbl.Controls.Add(rw);
}
you need to reinitialize the cell variable for each field
ASKER
gmailjini,
the last one works except that I need it to be under the title in another row, not in cell/column. thanks!
the last one works except that I need it to be under the title in another row, not in cell/column. thanks!
create a dataset and add one datatable to it..
named as "NewsItem"
and add colums to it. 1)"Title"
2)"Summary"
3)"Details"
then you have to called just a one method of NewsItemDataTabel.ReadXML( "xmlfilepa th");
named as "NewsItem"
and add colums to it. 1)"Title"
2)"Summary"
3)"Details"
then you have to called just a one method of NewsItemDataTabel.ReadXML(
so you need to add a column captions for the table ?
ASKER
Now it is like this: see (1)
-------------------------- ---------
| title | summary |
-------------------------- ---------
| title | summary |
-------------------------- ---------
I need it to be like this
-------------------------- ---------
| title |
-------------------------- ---------
| summary |
-------------------------- ---------
-------------------------- ---------
| title |
-------------------------- ---------
| summary |
-------------------------- ---------
thanks!
--------------------------
| title | summary |
--------------------------
| title | summary |
--------------------------
I need it to be like this
--------------------------
| title |
--------------------------
| summary |
--------------------------
--------------------------
| title |
--------------------------
| summary |
--------------------------
thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
that works. Just one quick other thing before I close and accept the answer.
when it parse a new paragraph from the xml, it doesn't leave the new line.
is there a way to leave the new line (parse it to the html).
thanks!
when it parse a new paragraph from the xml, it doesn't leave the new line.
is there a way to leave the new line (parse it to the html).
thanks!
Here is the program for you...check it.
Regards,
nishant
Regards,
nishant
DSNewsItems.NewsItemDataTable dtnewsitem = new DSNewsItems.NewsItemDataTable();
dtnewsitem.ReadXml("NewsSrc.xml");
yes you can add a blank row to the table just like below
rw = new TableRow();
cell = new TableCell();
rw.Cells.Add(cell);
cell = new TableCell();
rw.Cells.Add(cell);
tbl.Controls.Add(rw);
rw = new TableRow();
cell = new TableCell();
rw.Cells.Add(cell);
cell = new TableCell();
rw.Cells.Add(cell);
tbl.Controls.Add(rw);
ASKER
Here is another related Q.
https://www.experts-exchange.com/questions/27438138/Get-newline-from-xml-with-c.html
https://www.experts-exchange.com/questions/27438138/Get-newline-from-xml-with-c.html
Create a new cell for 'title' and 'summary' text.
Add the rows to Table.Rows, e.g.: tbl.Rows.Add(rw);