Methods for generating XML using ASP. NET

Source: Internet
Author: User

XML generated by ASP. NET is a common function. XML can transmit more data and information. The XML generated by ASP. NET starts from the business logic layer, data access layer, and presentation layer, and is presented as code.

1. business logic layer:

 
 
  1. public DataSet ABCData()  
  2.         {  
  3.             DataBase db = new DataBase();  
  4.             string sql = "select * from products where products_Year='2008'";  
  5.             DataSet ds = db.GetDataSet(sql);  
  6.             return ds;  
  7.         } 

Note:

Produts_Id products_Name products_Quantity products_Year
38 AAA 7649 2008
39 BBB 4567 2008
40 CCC 6410 2008

2. Data access layer:

3. Presentation Layer:

 
 
  1. Protected void Page_Load (object sender, EventArgs e)
  2. {
  3. DataBindDatabind=NewDataBind ();
  4. DataSetDs=Databind. ABCData ();
  5.  
  6. StringPlot_type="CategorizedVertical";
  7. XmlDocumentDoc=NewXmlDocument ();
  8. XmlElementNode=Doc. CreateElement ("anychart"); // create an anychart Node
  9. Doc. AppendChild (Node );
  10.  
  11. XmlElementNode1=Doc. CreateElement ("charts"); // create the node anychart subnode charts
  12. Doc. DocumentElement. AppendChild (Node1 );
  13.  
  14. XmlElementNode2=Doc. CreateElement ("chart"); // create a node charts subnode chart
  15. Node2.SetAttribute ("plot_type", plot_type); // Add the plot_type attribute to the node chart
  16. Node1.AppendChild (Node2 );
  17.  
  18. XmlElementNode3=Doc. CreateElement ("data"); // create the first subnode data of the node chart
  19. Node2.AppendChild (Node3 );
  20.  
  21. XmlElementNode4=Doc. CreateElement ("chart_settings"); // create the second subnode chart_settings of the node chart.
  22. Node2.AppendChild (Node4 );
  23.  
  24. XmlElementNode5=Doc. CreateElement ("series"); // create a node data subnode series
  25. Node5.SetAttribute ("name", "Year 2003"); // Add the first attribute name for the series Node
  26. Node5.SetAttribute ("type", "Bar"); // Add the second attribute type for the series Node
  27. Node3.AppendChild (Node5 );
  28. For (intI=1; I<= 3; I ++)
  29. {
  30. XmlElementNode13=Doc. CreateElement ("point"); // create a subnode point in node series
  31. Node13.SetAttribute ("name", ds. tables [0]. rows [I-1] ["products_Name"]. toString (); // Add the attribute name for the point node and set ds. in Tables [0], the products_Name column is extracted row by row and assigned to the attribute name.
  32. Node13.SetAttribute ("y", ds. tables [0]. rows [I-1] ["products_Quantity"]. toString (); // Add attribute y for the point node and set ds. in Tables [0], a column of products_Quantity data is retrieved row by row and assigned to attribute y.
  33. Node5.AppendChild (Node13 );
  34. }
  35.  
  36. XmlElementNode6=Doc. CreateElement ("title ");
  37. Node4.AppendChild (Node6 );
  38.  
  39. XmlElementNode7=Doc. CreateElement ("axes ");
  40. Node4.AppendChild (Node7 );
  41.  
  42. XmlElementNode8=Doc. CreateElement ("y_axis ");
  43. Node7.AppendChild (Node8 );
  44.  
  45. XmlElementNode9=Doc. CreateElement ("x_axis ");
  46. Node7.AppendChild (Node9 );
  47.  
  48. XmlElementNode10=Doc. CreateElement ("title ");
  49. Node10.InnerText="Sales"; // Assign the Sales value to the node title
  50. Node8.AppendChild (Node10 );
  51.  
  52. XmlElementNode11=Doc. CreateElement ("title ");
  53. Node11.InnerText="Retail Channel";
  54. Node9.AppendChild (Node11 );
  55.  
  56. XmlElementNode12=Doc. CreateElement ("text ");
  57. Node12.InnerText="Sales";
  58. Node6.AppendChild (Node12 );
  59. Doc. Save (Server. MapPath ("test. xml"); // Save the xml
  60. }

4. generated xml:

 
 
  1. <Anychart> 
  2. <Charts> 
  3. <Chart Plot_type="CategorizedVertical"> 
  4. <Data> 
  5. <Series Name=& Quot; Year 2003 & quot" Type="Bar"> 
  6. <Point Name="AAA" Y="7649" /> 
  7. <Point Name="BBB" Y="4567" /> 
  8. <Point Name="CCC" Y="6410" /> 
  9. </Series> 
  10. </Data> 
  11. <Chart_settings> 
  12. <Title> 
  13. <Text>Sales<Text> 
  14. </Title> 
  15. <Axes> 
  16. <Y_axis> 
  17. <Title>Sales</Title> 
  18. </Y_axis> 
  19. <X_axis> 
  20. <Title>Retail Channel</Title> 
  21. </X_axis> 
  22. </Axes> 
  23. </Chart_settings> 
  24. </Chart> 
  25. </Charts> 
  26. </Anychart> 

Some methods for generating XML in ASP. NET are introduced here.

Original article title: XML with complex layers generated by ASP. NET

Link: http://www.cnblogs.com/jiaojiaojojo/archive/2009/09/04/1559963.html

  1. In-depth research on Repeater controls: Maximum Flexibility
  2. Getting started with DataList controls
  3. Exploring the operation mechanism of the DataGrid Web Control
  4. Discuss the similarity between ASP. NET data Web controls
  5. Transformation from traditional ASP to ASP. NET: Understanding controls

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.