Hi,
You can have a method in your page which can return you the list elements.
I have put them in Page_load.
----------------For Webforms-------------
protected void Page_Load(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("XMLFile.xml"));
Console.WriteLine(doc.OuterXml);
var rootNode = doc.DocumentElement;
string list = string.Empty;
for (int i = 0; i < rootNode.ChildNodes.Count; i++)
{
var node = rootNode.ChildNodes[i];
list += "<li><a href=\"" + new ListItem(node.Attributes[0].Value) + "\">" + node.Attributes[1].Value + "</a></li>";
}
Response.Write(list);
}
---------------------For MVC--------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml;
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult GetMenu()
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("XMLFile.xml"));
var rootNode = doc.DocumentElement;
string list = string.Empty;
Dictionary<string, string> menu = new Dictionary<string, string>();
for (int i = 0; i < rootNode.ChildNodes.Count; i++)
{
var node = rootNode.ChildNodes[i];
menu.Add(node.Attributes[0].Value, node.Attributes[1].Value);
}
return PartialView(menu);
}
}
}
---Partial view (GetMenu.cshtml)--
@model Dictionary<string, string>
@foreach (var node in Model)
{
<li><a href="@node.Key"> + @node.Value</a></li>
}
--Index.cshtml (for home controller)
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@Html.Action("GetMenu", "Home")
Thanks
Srikant