一、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,文中要有疏漏之处请多指教。