实现的C#.net代码:
using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.IO; using System.Text; using System.Text.RegularExpressions; using System.Data.SqlClient; using System.Configuration;
//导入自定义的类库 using _3Layer.DataLayer.DataCommon; using _3Layer.DataLayer.DataCommon.DataAccess; using Library.ClassLibrary.Crypt.DES;
namespace CHONGCHONG.XML public class RenderingXML : System.Web.UI.Page { /// <summary> /// 从数据库预生成XML数据源 /// </summary> private void PreRenderXML() { string strSQL = "select语句略去........................; myDataLayer.Open();
RenderingXml="<?xml version='1.0' encoding='gb2312'?>\r\n"; RenderingXml+="<xml>\r\n"; try { System.Data.SqlClient.SqlDataReader myDR = (SqlDataReader)myDataLayer.ExecuteReader( strSQL ); while(myDR.Read()) { RenderingXml+="<TreeNode id='"+myDR["BoardID"]+"'>\r\n"; RenderingXml+="<NodeText>"+myDR["BoardName"]+"</NodeText>\r\n"; RenderingXml+="<title>"+myDR["Title"]+"</title>\r\n"; RenderingXml+="<NodeUrl>"+EncodeHTML ( EncodeParameter( myDR["Link"].ToString() ) )+"</NodeUrl>\r\n"; RenderingXml+="<child>"+myDR["children"]+"</child>\r\n"; RenderingXml+="<target>"+myDR["Target"]+"</target>\r\n"; RenderingXml+="</TreeNode>\r\n"; } } catch(System.Data.SqlClient.SqlException ee) { return ; } finally { myDataLayer.Close() ; } RenderingXml+="</xml>"; byte[] bytResult = Encoding.Default.GetBytes( RenderingXml ) ; Response.ContentType = "text/xml" ; Response.BinaryWrite( bytResult ) ; }
/// <summary> /// Description:加密路径参数 /// </summary> /// <param name="sourParameter"></param> /// <returns></returns> private string EncodeParameter( string sourParameter ) { string startString = String.Empty ; string endString = String.Empty ; StringBuilder destParameter = new StringBuilder() ;
if( sourParameter == null || sourParameter.Equals("") ) { destParameter.Append( String.Empty ).ToString() ; } else { //开始分析路径里的?字符 if( sourParameter.IndexOf("?")<0 ) { destParameter.Append( sourParameter ).ToString() ; } else { //以?号分割路径 string[] paramPath = sourParameter.Split( new char[]{'?'} ) ; startString = paramPath[0].ToString() ; endString = paramPath[1].ToString() ;
//开始分析路径里的&字符 if(sourParameter.IndexOf("&")<0) { //只有一个参数,用=号分割,直接把NameValue进行Des加密 string[] paramNameValue = endString.Split( new char[]{'='} ) ; string paramName = myDES.Encrypt ( paramNameValue[0].ToString() ,myDESKey ) ; string paramValue = myDES.Encrypt ( paramNameValue[1].ToString() ,myDESKey ) ;
destParameter.Append( startString ).Append("?"). Append( paramName ).Append("=").Append( paramValue ) ; } else { //有多个参数,以&号分割?号后面的路径 string[] paramJoin = endString.Split( new char[]{'&'} ) ; destParameter.Append( startString ).Append("?"). Append( EncoderNameValue( paramJoin ) ) .ToString() ; } } } return destParameter.ToString() ;
}
/// <summary> /// Description:加密路径里的NameValue参数 /// </summary> /// <param name="sourNameValue"></param> /// <returns></returns> private string EncoderNameValue( string[] sourNameValue ) { string[] paramNameValue ; string paramName ; string paramValue ; StringBuilder sb = new StringBuilder() ;
for( int i = 0 ; i <= sourNameValue.Length-1 ; i++ ) { //以=号分割每个NameValue参数 paramNameValue = sourNameValue[i].Split( new char[]{'='} ) ;
//开始对NameValue加密 paramName = myDES.Encrypt( paramNameValue[0].ToString() ,myDESKey ) ; paramValue = myDES.Encrypt( paramNameValue[1].ToString() ,myDESKey ) ;
//存储加密后的路径字符串 sb.Append( paramName ).Append("=").Append( paramValue ) ;
//是否最后一个NameValue参数,若不是在路基里添加&参数连接符 if( i<sourNameValue.Length ) { sb.Append("&") ; } }
return sb.ToString() ;
}
}
|