亲宝软件园·资讯

展开

c#递归生成XML c#递归生成XML实例

人气:0
想了解c#递归生成XML实例的相关内容吗,在本文为您仔细讲解c#递归生成XML的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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#程序设计有所帮助。

加载全部内容

相关教程
猜你喜欢
用户评论