c#递归生成XML实例

前端技术 2023/09/03 C#

本文实例讲述了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\");
           
        }
    }
}

本文地址:https://www.stayed.cn/item/7572

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。