如何获取顶级菜单表单Xml文件的项目 [英] How I Can Get Item For Top Menu Form Xml File
本文介绍了如何获取顶级菜单表单Xml文件的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
我有文件Topmenu.xml,如
<? xml version = 1.0 encoding = utf-8 ? >
< 项目 >
< item 文字 = 主页 NavigateUrl = #templatemo-Home / >
< item 文本 = 产品 NavigateUrl = < span class =code-keyword>#templatemo-Products / >
< item 文字 = 下载 NavigateUrl = #templatemo-Downloads / >
< item 文字 = 博客 NavigateUrl = #templatemo-blog < span class =code-keyword> / >
< / items >
我想把它拉到我的网页上,如:
< li > < a href = #templatemo-Home > 主页< / a > ; < / li >
< li > < a href = #templatemo-Products > 产品< / a > < / li >
< li > < a href = #templatemo-Downloads > 下载< / a > < / li >
< li > < a < span class =code-attribute> href = #templatemo-blog > 博客< / a > < / li >
我认为它可能是这样的:
@ foreach( var item in item_from_xmlFi le)
{
< li>< a href = item.NavigateUrl>item.Name< / a > < / li >
}
我使用ASP.NET MVC Razor。
我怎么做。
对不起,我的英语非常糟糕!
解决方案
< blockquote>
你可以在你的页面中有一个方法可以返回列表元素。
我把它们放在Page_load中。
----------------对于Webforms -------------
protected void Page_Load( object sender,EventArgs e)
{
XmlDoc ument 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 ]。值)+ \> + node.Attributes [ 1 ]。值+ < / a>< / li>;
}
Response.Write(list);
}
---------------------对于MVC ------- -------------------
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Mvc;
使用 System.Xml;
命名空间 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 字典< string,string>();
for ( int i = 0 ; i < rootNode.ChildNodes.Count; i ++)
{
var node = rootNode.ChildNodes [i];
menu.Add(node.Attributes [ 0 ]。值,node.Attributes [ 1 ]。值);
}
return PartialView(菜单);
}
}
}
---部分视图(GetMenu。 cshtml) -
@ model Dictionary< string,string>
@foreach( var 节点 in 模型)
{
< ; li>< a href = @ node.Key > + @ node.Value < / a > < / li >
}
--Index.cshtml(适用于家庭控制器)
@ {
ViewBag.Title =Index;
}
< h2 > 索引< / h2 > ;
@ Html.Action(GetMenu,Home)
谢谢
Srikant
Hello,
I have file Topmenu.xml like
<?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:
<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:
@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!
解决方案
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
这篇关于如何获取顶级菜单表单Xml文件的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文