Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have file Topmenu.xml like
XML
<?xml version="1.0" encoding="utf-8" ?>
<items>
  <item Text="Home" NavigateUrl="#templatemo-Home" />
  <item Text="Products" NavigateUrl="#templatemo-Products"  />
  <item Text="Downloads" NavigateUrl="#templatemo-Downloads"  />
  <item Text="Blog" NavigateUrl="#templatemo-blog"  />
</items>


And i want pull it on my webpage like:

HTML
<li><a href="#templatemo-Home">Home</a></li>
<li><a href="#templatemo-Products">Products</a></li>
<li><a href="#templatemo-Downloads">Downloads</a></li>
<li><a href="#templatemo-blog">Blog</a></li>


I think it maybe like:
C#
@foreach (var item in item_from_xmlFile)
{
  <li><a href="item.NavigateUrl">item.Name</a></li>
}


I using ASP.NET MVC Razor.

How i can do it.

Sorry, my english very bad!
Posted
Updated 5-Dec-14 5:10am
v2

1 solution

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-------------

C#
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--------------------------

C#
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)--
C#
@model Dictionary<string, string>
@foreach (var node in Model)
{
    <li><a href="@node.Key"> + @node.Value</a></li>
}


--Index.cshtml (for home controller)
Razor
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
@Html.Action("GetMenu", "Home")


Thanks
Srikant
 
Share this answer
 
v3
Comments
GinCanhViet 5-Dec-14 13:09pm    
Hi Srikant!
Thank you so much, for the code read XML.
But i mean MVC ASP.net.
It have no Page_load event :D
SrikantSahu 5-Dec-14 13:21pm    
Hi,
With MVC your job is lot easier,
Simply what you can do is, create an action method to return a JsonResult. While going through the loop just add the items to a List or dictionary object and return it from the action method.

Now that result can be consumed through an ajax call easily using Jquery.

or.
Create a partial view which returns only the LI part by following as below mentioned.
In the partial view you can bind the list read in the action method as a Model and using a foreach loop you can construct the LI.
Finally the partial view you can use it your view or in your layouts.
Thanks
GinCanhViet 5-Dec-14 13:29pm    
I had try it 2day :D
It's not easier with me.
Thank you!
SrikantSahu 5-Dec-14 14:47pm    
Updated the code to support MVC :)
GinCanhViet 6-Dec-14 0:19am    
:D wow, thank you!
Thank you so much!
Thank!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900