ASP.NET生成层次较复杂的XML

.业务逻辑层:

public DataSet ABCData()         {             DataBase db = new DataBase();             string sql = "select * from products where products_Year='2008'";             DataSet ds = db.GetDataSet(sql);             return ds;         }

注sql执行结果: produts_Id products_Name products_Quantity products_Year 38          AAA            7649               2008 39          BBB            4567               2008 40          CCC            6410               2008

2.数据访问层略:

3.表示层:

    protected void Page_Load(object sender, EventArgs e)     {         DataBind databind=new DataBind ();         DataSet ds=databind.ABCData();

        string plot_type = "CategorizedVertical";         XmlDocument doc = new XmlDocument();         XmlElement Node = doc.CreateElement("anychart");//创建一个anychart节点         doc.AppendChild(Node);

        XmlElement Node1 = doc.CreateElement("charts");//创建节点anychart子节点charts         doc.DocumentElement.AppendChild(Node1);

        XmlElement Node2 = doc.CreateElement("chart");//创建节点charts子节点chart         Node2.SetAttribute("plot_type", plot_type);//为节点chart添加plot_type属性         Node1.AppendChild(Node2);

        XmlElement Node3 = doc.CreateElement("data");//创建节点chart第一个子节点data         Node2.AppendChild(Node3);

        XmlElement Node4 = doc.CreateElement("chart_settings");//创建节点chart第二个子节点chart_settings         Node2.AppendChild(Node4);

        XmlElement Node5 = doc.CreateElement("series");//创建节点data子节点series         Node5.SetAttribute("name", "Year 2003");//为series节点添加第一个属性name         Node5.SetAttribute("type", "Bar");//为series节点添加第二个属性type         Node3.AppendChild(Node5);         for (int i = 1; i <= 3;i++ )         {             XmlElement Node13 = doc.CreateElement("point");//在节点series中创建子节点point             Node13.SetAttribute("name", ds.Tables[0].Rows[i - 1]["products_Name"].ToString());//为point节点添加属性name并将ds.Tables[0]中products_Name一列数据逐行取出,赋值给属性name             Node13.SetAttribute("y", ds.Tables[0].Rows[i - 1]["products_Quantity"].ToString());//为point节点添加属性y并将ds.Tables[0]中products_Quantity一列数据逐行取出,赋值给属性y             Node5.AppendChild(Node13);         }

        XmlElement Node6 = doc.CreateElement("title");         Node4.AppendChild(Node6);

        XmlElement Node7 = doc.CreateElement("axes");         Node4.AppendChild(Node7);

        XmlElement Node8 = doc.CreateElement("y_axis");         Node7.AppendChild(Node8);

        XmlElement Node9 = doc.CreateElement("x_axis");         Node7.AppendChild(Node9);

        XmlElement Node10 = doc.CreateElement("title");         Node10.InnerText = "Sales";//为节点title赋值Sales         Node8.AppendChild(Node10);

        XmlElement Node11 = doc.CreateElement("title");         Node11.InnerText = "Retail Channel";         Node9.AppendChild(Node11);

        XmlElement Node12 = doc.CreateElement("text");         Node12.InnerText = "销售";         Node6.AppendChild(Node12);         doc.Save(Server.MapPath("test.xml")); //保存xml     }

4.生产的xml:

<anychart> <charts>     <chart plot_type="CategorizedVertical">       <data>         <series name="Year 2003" type="Bar">           <point name="AAA" y="7649" />           <point name="BBB" y="4567" />           <point name="CCC" y="6410" />         </series>       </data>       <chart_settings>         <title>           <text>销售<text>         </title>         <axes>           <y_axis>             <title>Sales</title>           </y_axis>           <x_axis>             <title>Retail Channel</title>           </x_axis>         </axes>       </chart_settings>     </chart> </charts> </anychart>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值