生成XML文件,並保存到本地文件


生成如下結構的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> 

 

代碼如下:

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("D:\test.xml"); //保存xml       
        }

如果想判斷一下文件D:\test.xml是否存在,可以通過如下的代碼進行:

string path = "D:\test.xml";
                if (Directory.Exists(path) == false)
                {
                    Directory.CreateDirectory(path);
                }

最后把path傳給doc.save(path)就OK了……


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM