C# 数据源生成多级xml

private void xml()
        {
            DataSet ds = ProdTypeManager.GetList(" t_type=2 and t_prenID=0");
            XmlDocument doc = new XmlDocument();
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            doc.AppendChild(dec);
            XmlElement root = doc.CreateElement("data");
            doc.AppendChild(root);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                XmlElement cmenu = doc.CreateElement("cmenu");
                XmlAttribute cnname = doc.CreateAttribute("cnname");
                cnname.Value = ds.Tables[0].Rows[i]["t_title"].ToString();
                cmenu.Attributes.Append(cnname);

                XmlAttribute link = doc.CreateAttribute("link");
                link.Value = "shop_list?typeID=" + ds.Tables[0].Rows[i]["t_id"].ToString();
             //   cmenu.Attributes.Append(link);
               root.AppendChild(cmenu);
              //  doc.AppendChild(cmenu);
                DataSet ds1 = ProdTypeManager.GetList(" t_type=2 and t_prenID="+ds.Tables[0].Rows[i]["t_id"].ToString());
                for (int j = 0; j < ds1.Tables[0].Rows.Count; j++)
                {
                    XmlElement bdment = doc.CreateElement("bdment");
                    XmlAttribute cnname1 = doc.CreateAttribute("cnname");
                    cnname1.Value = ds1.Tables[0].Rows[j]["t_title"].ToString();
                    bdment.Attributes.Append(cnname1);

                    XmlAttribute link1 = doc.CreateAttribute("link");
                    link1.Value = "shop_list?typeID=" + ds1.Tables[0].Rows[j]["t_id"].ToString();
                    bdment.Attributes.Append(link1);
                  //  root.AppendChild(bdment);
                    cmenu.AppendChild(bdment);
                    DataSet ds2 = ProdTypeManager.GetList(" t_type=2 and t_prenID=" + ds1.Tables[0].Rows[j]["t_id"].ToString());
                    for (int  k = 0; k < ds2.Tables[0].Rows.Count; k++)
                    {
                        XmlElement ddment = doc.CreateElement("ddment");
                        XmlAttribute cnname2 = doc.CreateAttribute("cnname");
                        cnname2.Value = ds2.Tables[0].Rows[k]["t_title"].ToString();
                        ddment.Attributes.Append(cnname2);

                        XmlAttribute link2 = doc.CreateAttribute("link");
                        link2.Value = "shop_list?typeID=" + ds2.Tables[0].Rows[k]["t_id"].ToString();
                        ddment.Attributes.Append(link2);
                      //  root.AppendChild(ddment);
                        bdment.AppendChild(ddment);
                    }
                }
            }
           string xmlString = doc.OuterXml;
           doc.Save(Server.MapPath("../../xml/hassssh.xml"));

        }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值