您的位置:首页 > 编程语言 > C#

C# 写XML例子,根据行政代码树生成XML

2008-01-03 22:50 393 查看
Db_Class DbClass;
XmlTextWriter tw;
//填充一个子项
private void FillOneXml(string Sqlstr, int leveled)
{
int num = Convert.ToInt16(DbClass.Db_Executequery("select count(*) from (" + Sqlstr + ")"));
if (num < 1) return;
OracleDataReader DataReader = DbClass.Db_CreateReader(Sqlstr);
try
{
while (DataReader.Read())
{
string objid = DataReader[0].ToString();
string objName = (string)DataReader[1];
tw.WriteStartElement("cantoncode" + leveled);
tw.WriteAttributeString("cantoncode", objid);
tw.WriteAttributeString("cantonname", objName);
Sqlstr = " Select cantoncode,cantonName from nc_cantoncode "+ " where parentcode =" + objid + " order by cantoncode";
FillOneXml(Sqlstr, leveled + 1);
tw.WriteEndElement();
}

}
finally
{
DataReader.Close();
}

}

private void button1_Click(object sender, EventArgs e)
{
DbClass = new Db_Class();
DbClass.Db_Conn();
try
{
string fileName = @"C:/cantoncode.xml";
tw = new XmlTextWriter(fileName, null);
tw.Formatting = Formatting.Indented;
tw.WriteStartDocument();
tw.WriteStartElement("cantoncode");
string Sqlstr = " Select cantoncode,cantonName from nc_cantoncode "
+ " where parentcode is null order by cantoncode ";
FillOneXml(Sqlstr, 1);
tw.WriteEndElement();
tw.WriteEndDocument();
tw.Flush();
tw.Close();
}
finally
{
DbClass.close();
}

}

//测试 闫磊 Email:Landgis@126.com,yanleigis@21cn.com 2006.10.11
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: