一种从数据库利用xml创建树型目录的方法(C#)(二)

发表于:2007-06-30来源:作者:点击数: 标签:
一、UI层处理: #region 重新生成手机服务栏目列表XML private void CreateMetoneTree() { xmlDoc_Metone = new XmlDocument(); new Town.Com.MobileServiceTypeCollection().GetMetoneTree(out ds); //生成根节点 XmlElement treeContentElement = xmlDoc


一、UI层处理:

#region 重新生成手机服务栏目列表XML

private void CreateMetoneTree()

{



xmlDoc_Metone = new XmlDocument();



new Town.Com.MobileServiceTypeCollection().GetMetoneTree(out ds);

//生成根节点

XmlElement treeContentElement = xmlDoc_Metone.CreateElement("NewDataSet");

xmlDoc_Metone.AppendChild(treeContentElement);

//递归生成频道的XML文档

for(int j=0;j<ds.Tables["Tree"].Rows.Count;j++)

{

if(Int32.Parse(ds.Tables["Tree"].Rows[j].ItemArray[2].ToString()) == 0)

{

this.BulidXmlTree(treeContentElement,j);

}

else

{

continue;

}

}

string fileName = Server.MapPath("Xml/MetoneTree.xml");

xmlDoc_Metone.Save(fileName);

}

#endregion



#region 构XML树

private void BulidXmlTree(XmlElement tempXmlElement,int location)

{

DataRow tempRow = ds.Tables["Tree"].Rows[location];

//生成Tree节点

XmlElement treeElement = xmlDoc_Metone.CreateElement("Tree");

tempXmlElement.AppendChild(treeElement);



this.AppendChildElement("MetoneID",tempRow.ItemArray[0].ToString(),treeElement);

this.AppendChildElement("MetoneName",tempRow.ItemArray[1].ToString(),treeElement);

this.AppendChildElement("ParentMetoneID",tempRow.ItemArray[2].ToString(),treeElement);

this.AppendChildElement("MetoneUrl",tempRow.ItemArray[3].ToString(),treeElement);



for(int i=0 ; i < ds.Tables["Tree"].Rows.Count;i++)

{

if(ds.Tables["Tree"].Rows[i].ItemArray[2].ToString() == tempRow.ItemArray[0].ToString())

{

this.BulidXmlTree(treeElement,i);

}

}

}

#endregion



#region 追加子节点

/// <summary>

/// 追加子节点

/// </summary>

/// <param name="strName">节点名字</param>

/// <param name="strInnerText">节点内容</param>

/// <param name="parentElement">父节点</param>

/// <param name="xmlDocument">XmlDocument对象</param>

private void AppendChildElement(string strName , string strInnerText , XmlElement parentElement, XmlDocument xmlDocument )

{

XmlElement xmlElement = xmlDocument.CreateElement(strName) ;

xmlElement.InnerText = strInnerText ;

parentElement.AppendChild(xmlElement);



}



/// <summary>

/// 使用默认的频道Xml文档

/// </summary>

/// <param name="strName"></param>

/// <param name="strInnerText"></param>

/// <param name="parentElement"></param>

private void AppendChildElement(string strName , string strInnerText , XmlElement parentElement )

{

AppendChildElement(strName,strInnerText,parentElement,xmlDoc_Metone);

}

#endregion



一点说明:感觉这些代码基本上都是自解释的,关键点都有注释,看看就明白的。

二、解析xml的xslt

<?xml version="1.0" encoding="gb2312"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" version="1.0" encoding="gb2312" indent="yes"/>

<xsl:template match="NewDataSet">

<xsl:apply-templates select="Tree"></xsl:apply-templates>

</xsl:template>

<xsl:template match="Tree">

<div ondragstart="return false" style="padding-left:20px;" >

<xsl:attribute name="onclick">selectedEntity = @#<xsl:value-of select="MetoneID" />@#;clickOnEntity(this);redirect(@#<xsl:value-of select="MetoneUrl" />@#);</xsl:attribute>

<xsl:attribute name="open">false</xsl:attribute>

<xsl:attribute name="id">

<xsl:value-of select="MetoneID" />

</xsl:attribute>

<xsl:attribute name="STYLE">

<xsl:if test="count(ancestor::node()) > 2">

display: none;

</xsl:if>

cursor: hand;

</xsl:attribute>

<table border="0" cellpadding="0" cellspacing="0" style="line-height:130%">

<tr>

<td >

<a href="{MetoneUrl}" target="main"><xsl:value-of select="MetoneName"></xsl:value-of></a>

</td>

</tr>

</table>

<xsl:apply-templates select="Tree"></xsl:apply-templates>

</div>

</xsl:template>



</xsl:stylesheet>

三、一个小技巧:用js把xml的数据用xslt解析后填充到固定的html块中,比如div,调用代码:initAdmin(@#MetoneTree.xml@#, @#submenu@#,@#MetoneTree.xslt@#),js代码如下:

var view;//中英文版本切换

var xmlDoc;//MSXML2.DOM对象

var selectedEntity;//选中接点ID号





function initAdmin(fileName, divTree,xsltFile, expID)//初始化树状结构

{

var xslDoc;

var strXml;

var objXml;

var xmlHttp;

var xslProc;

var xslTemplate;



//alert(fileName);



xmlDoc = new ActiveXObject("MSXML2.DOMDocument");

xmlDoc.async = false;



xslDoc = new ActiveXObject("MSXML2.FreeThreadedDOMDocument");

xslDoc.async = false;





xmlDoc.load("XML/"+fileName);

xslDoc.load("XSLT/"+xsltFile);



xslTemplate = new ActiveXObject("MSXML2.XSLTemplate");

xslTemplate.stylesheet = xslDoc;

xslProc = xslTemplate.createProcessor();

xslProc.input = xmlDoc;

xslProc.transform();



document.all[divTree].innerHTML = xslProc.output;

if(expID != null)

{

expandSome(document.all[expID]);

}

}



四、如果有不明白的msn上提问好了,msn:yubo@x263.net,文中要有疏漏之处请多指教。

原文转自:http://www.ltesting.net