C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > c#递归生成XML

c#递归生成XML实例

投稿:shichen2014

这篇文章主要介绍了c#递归生成XML的方法,以实例形式较为详细的介绍了C#的递归算法与XML操作技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了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#程序设计有所帮助。

您可能感兴趣的文章:
阅读全文