0

I'm trying create a function to generate HTML file for multi-language menu. The code can work but only problem is its taking too long to compile. The flow is, this function will be called in the Global.asax file (process before start the program)

I suspect it caused by the looping process. So please advice me about the optimization. Thank you in advance. Here is the code.

 public static void GenerateMenu(string strPath)
{
    string[] tempCateHTML = new string[] { "_temp_menu_en.cshtml", "_temp_menu_bm.cshtml", "_temp_menu_ch.cshtml" }; //temporary file names
    string[] cateHTML = new string[] { "_menu_en.cshtml", "_menu_bm.cshtml", "_menu_ch.cshtml" };
    for (int i = 0; i < 3; i++)
    {

        using (EFContext ctx = new EFContext())
        {
            List<int> parentId = new List<int>();
            List<Category> parentCategory = ctx.Database.SqlQuery<Category>
                ("select CategoryId, category_name, category_nameBM, category_nameCH from dbo.lCategories where thread_parent = 0").ToList();

            StringWriter sWriter = new StringWriter();
            using (HtmlTextWriter wt = new HtmlTextWriter(sWriter))
            {
                 foreach (Category cate in parentCategory)
                {

                    string[] columnParent = new string[] { cate.Category_Name, cate.Category_NameBM, cate.Category_NameCH };
                    wt.RenderBeginTag(HtmlTextWriterTag.Li); //<li>
                    wt.AddAttribute(HtmlTextWriterAttribute.Href, "#");
                    wt.RenderBeginTag(HtmlTextWriterTag.A); //<a>
                    wt.Write(columnParent[i]);
                    wt.RenderEndTag();//</a>

                    wt.RenderBeginTag(HtmlTextWriterTag.Ul);//<ul>

                    List<Category> childCategoryL1 = ctx.Database.SqlQuery<Category>
                        ("select CategoryId, category_name, category_nameBM, category_nameCH from dbo.lCategories where thread_parent = {0}", cate.CategoryID).ToList();

                   foreach (Category root in childCategoryL1)
                    {
                        string[] columnChild1 = new string[] { root.Category_Name, root.Category_NameBM, root.Category_NameCH };
                        wt.RenderBeginTag(HtmlTextWriterTag.Li);//<li>
                        wt.AddAttribute(HtmlTextWriterAttribute.Href, "#");
                        wt.RenderBeginTag(HtmlTextWriterTag.A);//<a>
                        wt.Write(columnChild1[i]);
                        wt.RenderEndTag();//</a>

                        List<Category> childCategoryL2 = ctx.Database.SqlQuery<Category>
                            ("select CategoryId, category_name, category_nameBM, category_nameCH from dbo.lCategories where thread_parent = {0}", root.CategoryID).ToList();

                        if (childCategoryL2.Count > 0)
                        {
                            wt.RenderBeginTag(HtmlTextWriterTag.Ul);//<ul>

                            foreach (Category child1 in childCategoryL2)
                            {
                                string[] columnChild2 = new string[] { child1.Category_Name, child1.Category_NameBM, child1.Category_NameCH };
                                wt.RenderBeginTag(HtmlTextWriterTag.Li);//<li>
                                wt.AddAttribute(HtmlTextWriterAttribute.Href, "#");
                                wt.RenderBeginTag(HtmlTextWriterTag.A);//<a>
                                wt.Write(columnChild2[i]);
                                wt.RenderEndTag();//</a>

                                List<Category> childCategoryL3 = ctx.Database.SqlQuery<Category>
                                    ("select CategoryId, category_name, category_nameBM, category_nameCH from dbo.lCategories where thread_parent = {0}", child1.CategoryID).ToList();

                                if (childCategoryL3.Count > 0)
                                {
                                    wt.RenderBeginTag(HtmlTextWriterTag.Ul);//<ul>

                                    foreach (Category child2 in childCategoryL3)
                                    {
                                        string[] columnChild3 = new string[] { child2.Category_Name, child2.Category_NameBM, child2.Category_NameCH };
                                        wt.RenderBeginTag(HtmlTextWriterTag.Li);//<li>
                                        wt.AddAttribute(HtmlTextWriterAttribute.Href, "#");
                                        wt.RenderBeginTag(HtmlTextWriterTag.A);//<a>
                                        wt.Write(columnChild3[i]);
                                        wt.RenderEndTag();//</a>
                                        wt.RenderEndTag();//</li>
                                    }
                                    wt.RenderEndTag();//</ul>
                                }
                                wt.RenderEndTag();//</li>
                            }

                            wt.RenderEndTag();//</ul>
                        }
                        wt.RenderEndTag();//</li>
                    }

                    wt.RenderEndTag();//</ul>
                    wt.RenderEndTag();//</li>
                }
            }


            string menuHTML = sWriter.ToString();

            string filePath = Path.Combine(strPath, @"Views\Shared\CacheData\CateMenu\");
            new FileInfo(filePath).Directory.Create(); //create new folder
            var menuPath = String.Format("{0}{1}", filePath, tempCateHTML[i]); //create multiple HTML files for multiple language

            using (FileStream fs = new FileStream(menuPath, FileMode.Append, FileAccess.Write))
            {
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine(menuHTML);
                sw.Flush();
                sw.Close();
                fs.Close();
            }

            if (!File.Exists(filePath + cateHTML[i]))
            {
                using (File.Create(filePath + cateHTML[i]))
                {
                    //create dummy file if the file doesnt exists
                }
            }

            File.Replace(menuPath, filePath + cateHTML[i], filePath + cateHTML[i] + ".bac");
        }
    }

}

it's a long process. So really Thank you for the time

3
  • 1
    That looks like far too many queries just to build a menu. Obviously I don't know the details of what you're doing, but I would suggest abandoning this approach and looking at doing all the work in a stored procedure using a recursive query to generate your menu structure Commented Aug 13, 2014 at 9:23
  • Thank you for the comment, actually I don't mind to abandon this method but I can't think of any better way(logic) for write HTML that get data from database to generate multi-level menu. Can please provide any example to do this. Commented Aug 14, 2014 at 2:39
  • www.google.co.uk/?gws_rd=ssl#q=asp.net+category+subcategory+menu Commented Aug 14, 2014 at 8:41

1 Answer 1

0

Actuall I've already found the solution for my problem. The solution was built tree menu based on this method. Thank you for the help.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.