国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

c#遞歸生成XML實例

2020-01-24 02:15:38
字體:
來源:轉載
供稿:網友

本文實例講述了c#遞歸生成XML的方法。分享給大家供大家參考。具體實現方法如下:

這里結合網上搜到的資料,寫了個遞歸生成xml,經過調試可以使用,數據庫結構如下圖所示:

代碼如下:

復制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
//using System.Data;
using System.Data.SqlClient;

namespace WindowsApplication1
{
    public partial class frmReadXML : Form
    {
        public frmReadXML()
        {
            InitializeComponent();
        }
        public string connstr = System.Configuration.ConfigurationManager.AppSettings["connstr"].ToString();
      
        private void frmReadXML_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(connstr);
            conn.Open();
            SqlCommand comm = new SqlCommand();
            comm.CommandText = "select * from Nationals";
            comm.Connection = conn;
            comm.CommandType = CommandType.Text;
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = comm;
            DataSet ds = new DataSet();
            sda.Fill(ds);

            XmlDocument doc = new XmlDocument();
            doc.AppendChild(doc.CreateXmlDeclaration("1.0","",""));
            XmlElement rootnode = doc.CreateElement("root");
            doc.AppendChild(rootnode);

            CreateXMLtree(ds,doc,"",(XmlElement)null);

        }
        DataRow[] dr;
       
        public void CreateXMLtree(DataSet ds, XmlDocument doc, string parentCode,XmlElement parentNode)
        {

            if (parentCode == "")
            {
                 dr = ds.Tables[0].Select("parentCode=''");
            }
            else
            {
                dr = ds.Tables[0].Select("parentCode='" + Convert.ToString(parentCode) + "'");
            }
            XmlElement tempNode;
            foreach (DataRow drv in dr)
            {
                if (parentCode == "")
                {
                    tempNode = doc.CreateElement("c"+drv["Code"].ToString()); //創(chuàng)建一級節(jié)點
                    tempNode.SetAttribute("name", drv["name"].ToString()); //創(chuàng)建屬性
                    //tempNode.InnerText = drv["name"].ToString();
                    doc.DocumentElement.AppendChild(tempNode);//添加一級節(jié)點
                    CreateXMLtree(ds,doc,drv["Code"].ToString(),tempNode);
                }
                else
                {
                    tempNode = doc.CreateElement("c"+drv["Code"].ToString().Replace(".", ""));
                    tempNode.SetAttribute("name", drv["name"].ToString());
                    //tempNode.InnerText = drv["name"].ToString();
                    parentNode.AppendChild(tempNode);
                    CreateXMLtree(ds, doc, drv["Code"].ToString(), tempNode);
                }
            }
            doc.Save(AppDomain.CurrentDomain.BaseDirectory+"/xxx.xml");
           
        }
    }
}

希望本文所述對大家的C#程序設計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 始兴县| 陵水| 肃北| 海阳市| 化德县| 峨山| 农安县| 河曲县| 宁南县| 江西省| 讷河市| 抚顺市| 达尔| 海原县| 遂宁市| 大英县| 洮南市| 合阳县| 石渠县| 正蓝旗| 兴化市| 连南| 治县。| 乐至县| 本溪| 临沧市| 桂平市| 大埔县| 庆云县| 衢州市| 历史| 惠东县| 娄烦县| 芒康县| 二连浩特市| 安吉县| 五常市| 镶黄旗| 定兴县| 榆中县| 新丰县|