ASP.NET生成XML是一种比较常见的功能,XML可以传递更多的数据与信息。这里的ASP.NET生成XML将从业务逻辑层、数据访问层、表示层入手,以代码的形式进行展现。
1.业务逻辑层:
- 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>
ASP.NET生成XML的一些方法就介绍到这里吧。
原文标题:ASP.NET生成层次较复杂的XML
链接:http://www.cnblogs.com/jiaojiaojojo/archive/2009/09/04/1559963.html
【编辑推荐】