asp.net c#遍历xml并以目录形式输出

作者:袖梨 2022-06-25
 代码如下 复制代码

   

   
   
     
     
     
     
     
     
     
   

   
     
     
       
       
       
       
       
       
       
       
       
   

  

然后在后台用递归读取XML结点,生成HTML显示到页面即可,实现如下:

 代码如下 复制代码

 

    protected void Page_Load(object sender, EventArgs e)
    {
        //XDocument doc = XDocument.Load(Server.MapPath("html5Reader/ReaderData.xml"));
        StringBuilder sb = new StringBuilder();
        XmlDocument dc = new XmlDocument();
        string path = Server.MapPath("~/html5Reader/ReaderData.xml");
        dc.Load(path);
        XmlNodeList xnl = dc.SelectNodes("section/chapter");
        sb.Append("

    ");
            readxml(xnl, sb);
            sb.Append("
");
        this.html.InnerHtml = sb.ToString();
    }

    private void readxml(XmlNodeList xmlnl,StringBuilder sb_)
    {
        foreach (XmlNode xl in xmlnl)
        {
            if (xl.ChildNodes.Count == 0)
            {
                sb_.Append("

  • " + xl.Attributes["value"].Value + "
  • ");
                }
                else
                {
                    sb_.Append("
  • " + xl.Attributes["value"].Value + "
      ");
                      readxml(xl.ChildNodes, sb_);
                      sb_.Append("
  • ");
                }
            }
        }


    前台很页面:

     代码如下 复制代码


       


       

       

       
       

       



    运行后

    相关文章

    精彩推荐