当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - How to change XML from dataset into HTML UL

问题描述:

I'm working on a C# webforms application and have a datalayer which gathers information about the menu a customer can see, based on their customer number and order type.

I was using the ASP.NET menu control for this until the qa department asked to change the menu to expand on click instead of hover. At that point, I decided to try and do the menu with a simpler css/html/jquery approach but I've hit a jam.

I have the following method in my datalayer that gets information for the menu and returns it as XML. What I'm stuck on is how to take the XML that was being gathered, when I was using the menu control and hopefully reformat it into a UL for using in the html/css approach I'd like to do.

public static string BuildMenu(string cprcstnm, string docType)

{

DataSet ds = new DataSet();

string connStr = ConfigurationManager.ConnectionStrings["DynamicsConnectionString"].ConnectionString;

using (SqlConnection conn = new SqlConnection(connStr))

{

string sql = "usp_SelectItemMenuByCustomer";

SqlDataAdapter da = new SqlDataAdapter(sql, conn);

da.SelectCommand.CommandType = CommandType.StoredProcedure;

da.SelectCommand.Parameters.Add("@CPRCSTNM", SqlDbType.VarChar).Value = cprcstnm;

da.SelectCommand.Parameters.Add("@DOCID", SqlDbType.VarChar).Value = docType;

da.Fill(ds);

da.Dispose();

}

ds.DataSetName = "Menus";

ds.Tables[0].TableName = "Menu";

DataRelation relation = new DataRelation("ParentChild",

ds.Tables["Menu"].Columns["MenuID"],

ds.Tables["Menu"].Columns["ParentID"],

false);

relation.Nested = true;

ds.Relations.Add(relation);

return ds.GetXml();

}

A sample of XMl that is output is as follows:

<Menus>

- <Menu>

<MenuID>23</MenuID>

<ITEMNMBR>0</ITEMNMBR>

<Text>ACC</Text>

<Description>ACC</Description>

<ParentID>0</ParentID>

- <Menu>

<MenuID>34</MenuID>

<ITEMNMBR>1</ITEMNMBR>

<Text>BASE</Text>

<Description>BASE</Description>

<ParentID>23</ParentID>

- <Menu>

<MenuID>516</MenuID>

<ITEMNMBR>2</ITEMNMBR>

<Text>HYP</Text>

<Description>HYP</Description>

<ParentID>34</ParentID>

I would need to convert this to something such as :

<ul class="dropdown">

<li><a href="#">ACC</a>

<ul class="sub_menu">

<li>

<a href="#">BASE</a>

<ul>

<li>

<a href="#">HYP</a>

<ul>

<li><a href="#">Terminal 1</a></li>

<li><a href="#">Terminal 1</a></li>

</ul>

</li>

</ul>

</li>

网友答案:

You will get some ideas from the following MSDN link that illustrates writing html from a dataset using xslt

http://msdn.microsoft.com/en-us/library/8fd7xytc(v=vs.80).aspx

网友答案:

There is no generic way to do it.Or some kind of GUI tool to make it for you.

You will have to manipulate your XML and convert it into equivalent( according to your need) html.

  • First I would suggest you, that if only you wanted to handle the click effect of MenuItems and CSS related issues, don't change it. you can very easily bind a jQuery function to your ASP Menu items. You can do so, very easily .Just add a css class to your asp:menuItem and grab it using jQuery like this:

    <asp:Menu ID="myMenu" runat="server">
    ..
    <asp:MenuItem NavigateUrl="" CssClass="menuItem" Text="Menu1" Value="Menu1"/>
    

    and grab it like

      $('.menuItem').click(function(){
             $(this).css({'width':'100px'});
      });
    
  • And if you are seriously to throw away your ASP Menu Control, but not the XML datasource for menus, then get ready for some serious string manipulation. do it like this.

    XmlDocument doc = new XmlDocument(xml);
    XmlNodeList menus = doc.SelectNodes("//Menus/Menu");
    
    string html ="<ul>";
    foreach(XmlNode menu in menus)
    {
       html += "<li><a href='#' >";
       html += menu.SelectSingleNode("Text").InnerText + "</a></li>";
    }
    html +="</ul>";
    
分享给朋友:
您可能感兴趣的文章:
随机阅读: