c#遞歸生成XML實(shí)例
本文實(shí)例講述了c#遞歸生成XML的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
這里結(jié)合網(wǎng)上搜到的資料,寫了個(gè)遞歸生成xml,經(jīng)過調(diào)試可以使用,數(shù)據(jù)庫結(jié)構(gòu)如下圖所示:
代碼如下:
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é)點(diǎn)
tempNode.SetAttribute("name", drv["name"].ToString()); //創(chuàng)建屬性
//tempNode.InnerText = drv["name"].ToString();
doc.DocumentElement.AppendChild(tempNode);//添加一級節(jié)點(diǎn)
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#程序設(shè)計(jì)有所幫助。
相關(guān)文章
Unity實(shí)現(xiàn)3D貪吃蛇的移動(dòng)代碼
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)3D貪吃蛇的移動(dòng)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04C#開發(fā)之int與string轉(zhuǎn)化操作
這篇文章主要介紹了C#開發(fā)之int與string轉(zhuǎn)化操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12C#實(shí)現(xiàn)ComboBox自動(dòng)匹配字符
本文介紹C#如何實(shí)現(xiàn)ComboBox自動(dòng)匹配字符1.采用CustomSource當(dāng)做提示集合2. 直接使用下拉列表中的項(xiàng)作為匹配的集合,需要了解的朋友可以參考下2012-12-12C# 獲取指定QQ頭像繪制圓形頭像框GDI(Graphics)的方法
某論壇的評論區(qū)模塊,發(fā)現(xiàn)這功能很不錯(cuò),琢磨了一晚上做了大致一樣的,用來當(dāng)做 注冊模塊 的頭像綁定功能,下面通過實(shí)例代碼給大家介紹下C# 獲取指定QQ頭像繪制圓形頭像框GDI(Graphics)的方法,感興趣的朋友一起看看吧2021-11-11Entity?Framework映射TPH、TPT、TPC與繼承類
這篇文章介紹了Entity?Framework映射TPH、TPT、TPC與繼承類,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06