Reputation: 255
I am new to asp.net and I want to display data in Bulleted list
.I am fetching items using following Foreach
loop and want to display in a Bulleted list
in asp.net.
Result.aspx.cs
foreach (XmlElement statement1 in node2statements.ChildNodes)
{
var st = statement1.InnerText.ToString();
}
Result.aspx
<ul id="section1">
<li>
</li>
</ul>
Upvotes: 2
Views: 2874
Reputation: 8359
I did a small example to demonstrate how to bind data from an xml file to a bulletedList. I used a XmlDataSource object. First I used an xml file
products.xml
<?xml version="1.0" encoding="utf-8" ?>
<list>
<product id="123">
<name>product a</name>
<price>123.45</price>
</product>
<product id="123">
<name>product b</name>
<price>123.45</price>
</product>
</list>
and I created a transform file - as only attributes will be accesible in controls. Credit goes to Darin Dimitrov for pointing out that
... a bug in XmlDataSource that prevents you to bind to values of xml nodes. It works with attributes only
transform.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="list">
<list>
<xsl:apply-templates select="product"/>
</list>
</xsl:template>
<xsl:template match="product">
<product>
<xsl:attribute name="id">
<xsl:value-of select="@id"/>
</xsl:attribute>
<xsl:attribute name="name">
<xsl:value-of select="name"/>
</xsl:attribute>
<xsl:attribute name="price">
<xsl:value-of select="price"/>
</xsl:attribute>
</product>
</xsl:template>
</xsl:stylesheet>
Then I did two examples - first one demonstrates how to use it within the page.
<form id="form1" runat="server">
<div>
<h3>Bulleted List with XML binding</h3>
<asp:BulletedList ID="blPageOnly" runat="server" DataValueField="id" DataTextField="name" DataSourceID="xmlSource"></asp:BulletedList>
<asp:XmlDataSource ID="xmlSource" runat="server" DataFile="~/app_data/products.xml" TransformFile="~/app_data/transform.xsl" XPath="list/product"></asp:XmlDataSource>
</div>
<div>
<h3>Bulleted List with XML binding - code behind</h3>
<asp:BulletedList ID="blCodeBehind" runat="server"></asp:BulletedList>
</div>
</form>
Second one shows how to bind xml file from code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
XmlDataSource xmlSource = new XmlDataSource()
{
DataFile = "~/app_data/products.xml",
XPath="list/product",
TransformFile = "~/app_data/transform.xsl"
};
blCodeBehind.DataSource = xmlSource;
blCodeBehind.DataTextField = "name";
blCodeBehind.DataValueField = "id";
blCodeBehind.DataBind();
}
}
Will result in
I hope you can adapt my code to help you out!
Upvotes: 1
Reputation: 629
Make use of asp:Repeater and bind it with your Child Nodes
Inside the item template of repeater you can define UL and LI and assign them to statement1
Upvotes: 0