当前位置:主页 > 软件编程 > .NET代码 >

c#递归生成XML实例

时间:2022-03-07 08:52:00 | 栏目:.NET代码 | 点击:

本文实例讲述了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()); //创建一级节点
                    tempNode.SetAttribute("name", drv["name"].ToString()); //创建属性
                    //tempNode.InnerText = drv["name"].ToString();
                    doc.DocumentElement.AppendChild(tempNode);//添加一级节点
                    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#程序设计有所帮助。

您可能感兴趣的文章:

相关文章