I have the following xml file and want to parse it into an .aspx table with c#. I already have a page with aspx and cs file. I have tried this and was getting an error. Any ideas. Thank you.
I also need to have each node to have a link to an html anchor where it can point to the item if possible.
XmlDocument Doc = new XmlDocument();
Doc.Load(Server.MapPath("NewsSrc.xml"));
XmlElement root = Doc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("/News");
if (!IsPostBack)
{
Table tbl = new Table();
tbl.ID = "table1";
tbl.BorderWidth = 1;
this.Controls.Add(tbl);
foreach (XmlNode node in nodes)
{
TableRow rw = new TableRow();
TableCell cell = new TableCell();
var element = (XmlElement)node;
var Title = element.Value; //["Title"];
NewsItemTxt.Text = Title; //NewsItemtxt is label;
cell.Controls.Add(NewsItemTxt);
rw.Cells.Add(cell);
tbl.Controls.Add(rw);
}
}
// and the xml file is //
<?xml version="1.0" encoding="utf-8" ?>
<!-- Edited by myself -->
<News>
<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>
</News>
Try this, it’s using System.Xml and System.Xml.Linq, it should work but I can’t guarantee it: