流量统计:
Rixu Blog (日需博客)
日需博客,每日必需来踩踩哦..
posts - 108,comments - 54,trackbacks - 0
<add key="Spider" value="Googlebot|Baiduspider|Yahoo! Slurp|YodaoBot|msnbot"/>

把上面的代码加入web.config文件的appSettings节点中,别问为什么...

为Request写个扩展:

RequestExtension
 1 namespace MusicStore.Utility
2 {
3 using System;
4 using System.Configuration;
5 using System.Web;
6
7 /// <summary>
8 /// HttpRequest扩展
9 /// </summary>
10 public static class RequestExtension
11 {
12 /// <summary>
13 /// 是不是网络爬虫的请求
14 /// </summary>
15 /// <param name="request">HttpRequest的实例</param>
16 /// <returns>是不是网络爬虫的请求(True|False)</returns>
17 public static bool IsSpiderRequest(this HttpRequest request)
18 {
19 string userAgent = request.UserAgent.ToLower();
20 string[] spiders = ConfigurationManager.AppSettings["Spider"].ToLower().Split('|');
21 foreach (string spider in spiders)
22 {
23 if (userAgent.Contains(spider))
24 {
25 return true;
26 }
27 }
28 return false;
29 }
30 }
31 }

看下xml文件:

Genres.xml
1 <?xml version="1.0" encoding="utf-8" ?>
2 <?xml-stylesheet type="text/xsl" href="Genres.xslt"?>
3 <root />

XSLT文件:

Genres.xslt
 1 <?xml version="1.0" encoding="utf-8"?>
2 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
3 <xsl:output method="html" indent="yes" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.1//EN" doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" omit-xml-declaration="yes"/>
4 <xsl:variable name="genresPath">http://localhost:4557/List/Genres.ashx</xsl:variable>
5 <xsl:variable name="genres" select="document($genresPath)/Genres" />
6 <xsl:variable name="itemUrl">http://localhost:4557/Item/</xsl:variable>
7 <xsl:template match="root">
8 <html xmlns="http://www.w3.org/1999/xhtml" >
9 <head>
10 <title>Demo</title>
11 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
12 </head>
13 <body>
14 <ul id="genres">
15 <xsl:for-each select="$genres/Genre">
16 <li>
17 <a>
18 <xsl:attribute name="href">
19 <xsl:value-of select="concat(concat($itemUrl,'Default.aspx?genreId='),GenreId)"/>
20 </xsl:attribute>
21 <xsl:attribute name="title">
22 <xsl:value-of select="Description"/>
23 </xsl:attribute>
24 <xsl:value-of select="Name"/>
25 </a>
26 </li>
27 </xsl:for-each>
28 </ul>
29 </body>
30 </html>
31 </xsl:template>
32 </xsl:stylesheet>

Genders.ashx文件内容:

Genres.ashx
 1 namespace MusicStore.List
2 {
3 using System;
4 using System.Collections.Generic;
5 using System.Text;
6 using System.Web;
7 using System.Web.Services;
8 using System.Xml.Serialization;
9 using MusicStore.BLL;
10 using MusicStore.Models;
11
12 /// <summary>
13 /// Genres 的摘要说明
14 /// </summary>
15 [WebService(Namespace = "http://tempuri.org/")]
16 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
17 public class Genres : IHttpHandler
18 {
19 public void ProcessRequest(HttpContext context)
20 {
21 context.Response.ContentType = "text/xml";
22 context.Response.ContentEncoding = Encoding.UTF8;
23
24 List<Genre> genres = new GenreBLL().GetAllGenres();
25 XmlSerializer serializer = new XmlSerializer(genres.GetType(), new XmlRootAttribute("Genres"));
26 serializer.Serialize(context.Response.OutputStream, genres);
27 }
28
29 public bool IsReusable
30 {
31 get
32 {
33 return false;
34 }
35 }
36 }
37 }

实体类文件:

Gender.cs
 1 namespace MusicStore.Models
2 {
3 using System;
4 using System.Collections.Generic;
5 using System.Xml;
6 using System.Xml.Serialization;
7
8 /// <summary>
9 /// 音乐类型
10 /// </summary>
11 [Serializable]
12 public class Genre
13 {
14 /// <summary>
15 /// 音乐类型Id
16 /// </summary>
17 public int? GenreId { get; set; }
18
19 /// <summary>
20 /// 音乐类型名称
21 /// </summary>
22 public string Name { get; set; }
23
24 /// <summary>
25 /// 音乐类型描述
26 /// </summary>
27 public string Description { get; set; }
28 }
29 }

实体类适配器类:

GenreModelAdapter.cs
 1 namespace MusicStore.Models.Adapters
2 {
3 using System;
4 using System.Data;
5
6 /// <summary>
7 /// 音乐类型适配器
8 /// </summary>
9 public class GenreModelAdapter:ModelAdapterBase<Genre>
10 {
11 /// <summary>
12 /// 音乐类型适配器构造方法
13 /// </summary>
14 public GenreModelAdapter()
15 : base(null)
16 { }
17
18 /// <summary>
19 /// 音乐类型适配器构造方法
20 /// </summary>
21 /// <param name="genre">音乐类型模型实例</param>
22 public GenreModelAdapter(Genre genre)
23 : base(genre)
24 { }
25
26 /// <summary>
27 /// 填充音乐类型实体
28 /// </summary>
29 /// <param name="dataReader">数据读取器</param>
30 /// <returns>填充后的音乐类型实体</returns>
31 public override Genre Fill(IDataReader dataReader)
32 {
33 Model.GenreId = dataReader["GenreId"] as int?; // 音乐类型Id
34 Model.Name = dataReader["Name"] as string; // 音乐类型名称
35 Model.Description = dataReader["Description"] as string; // 音乐类型描述
36
37 return Model;
38 }
39 }
40 }

实体适配器基类:

ModelAdapterBase.cs
 1 namespace MusicStore.Models.Adapters
2 {
3 using System;
4 using System.Data;
5
6 /// <summary>
7 /// 模型适配器基类
8 /// </summary>
9 public abstract class ModelAdapterBase<T> where T : class
10 {
11 /// <summary>
12 /// 模型适配器实体
13 /// </summary>
14 public T Model;
15
16 /// <summary>
17 /// 模型适配器基类器构造方法
18 /// </summary>
19 /// <param name="model">模型实例</param>
20 public ModelAdapterBase(T model)
21 {
22 Model = model;
23 }
24
25 /// <summary>
26 /// 填充实体
27 /// </summary>
28 /// <param name="dataReader">数据读取器</param>
29 /// <returns>填充后的实体</returns>
30 public abstract T Fill(IDataReader dataReader);
31 }
32 }

数据访问类:

GenreDAL.cs
 1 namespace MusicStore.DAL
2 {
3 using System;
4 using System.Collections.Generic;
5 using System.Web;
6 using MusicStore.Models;
7 using MusicStore.Models.Adapters;
8 using System.Data.SqlClient;
9 using MusicStore.DAL.Utility;
10
11 /// <summary>
12 /// 音乐类型数据访问类
13 /// </summary>
14 public class GenreDAL
15 {
16 /// <summary>
17 /// 获取所有音乐类型
18 /// </summary>
19 /// <returns>音乐类型实体列表</returns>
20 public List<Genre> GetAllGenres()
21 {
22 string storedProcedureName = "GetAllGenres";
23
24 GenreModelAdapter genreAdapter = new GenreModelAdapter();
25 List<Genre> genres = new List<Genre>();
26
27 SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.musicStoreConnectionString, storedProcedureName, null);
28
29 if (reader == null)
30 {
31 return null;
32 }
33
34 while (reader.Read())
35 {
36 Genre genre = new Genre();
37 genreAdapter.Model = genre;
38 genreAdapter.Fill(reader);
39 genres.Add(genre);
40 }
41
42 return genres;
43 }
44 }
45 }

业务类:

GenreBLL.cs
 1 namespace MusicStore.BLL
2 {
3 using System;
4 using System.Collections.Generic;
5 using MusicStore.DAL;
6 using MusicStore.Models;
7
8 /// <summary>
9 /// 音乐类型业务逻辑类
10 /// </summary>
11 public class GenreBLL
12 {
13 /// <summary>
14 /// 获取所有音乐类型
15 /// </summary>
16 /// <returns>音乐类型实体列表</returns>
17 public List<Genre> GetAllGenres()
18 {
19 GenreDAL genreDAL = new GenreDAL();
20 return genreDAL.GetAllGenres();
21 }
22 }
23 }


Default.ashx:

Default.ashx
 1 namespace MusicStore.List
2 {
3 using System;
4 using System.Web;
5 using System.Web.Services;
6 using System.Xml;
7 using System.Xml.Xsl;
8 using MusicStore.Utility;
9
10 /// <summary>
11 /// $codebehindclassname$ 的摘要说明
12 /// </summary>
13 [WebService(Namespace = "http://tempuri.org/")]
14 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
15 public class Default : IHttpHandler
16 {
17
18 public void ProcessRequest(HttpContext context)
19 {
20 string xmlFileName = "Genres.xml";
21 string xmlPath = context.Server.MapPath(xmlFileName);
22
23 if (!context.Request.IsSpiderRequest())
24 {
25 context.Response.Redirect(xmlFileName);
26 return;
27 }
28
29 string xslPath = context.Server.MapPath("Genres.xslt");
30
31 XslCompiledTransform xslt = new XslCompiledTransform();
32
33 XsltSettings xsltSettings = new XsltSettings();
34 xsltSettings.EnableDocumentFunction = true;
35 xsltSettings.EnableScript = true;// 转换时允许执行xslt中的脚本
36
37 xslt.Load(xslPath, xsltSettings, new XmlUrlResolver());
38 xslt.Transform(xmlPath, null, context.Response.OutputStream);
39 }
40
41 public bool IsReusable
42 {
43 get
44 {
45 return false;
46 }
47 }
48 }
49 }

别晕,看下解决方案:



运行结果:

浏览器请求返回的结果
1 <?xml version="1.0" encoding="utf-8" ?>
2 <?xml-stylesheet type="text/xsl" href="/List/Genres.xslt"?>
3 <root />
爬虫请求返回的结果
 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml">
3 <head>
4 <title>Demo</title>
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6 </head>
7 <body>
8 <h1>Xslt Demo</h1>
9 <ul id="genres">
10 <li>
11 <a href="http://localhost:4557/Item/Default.aspx?genreId=1" title="rock Description...">Rock</a>
12 </li>
13 <li>
14 <a href="http://localhost:4557/Item/Default.aspx?genreId=1" title="Jazz Description...">Jazz</a>
15 </li>
16 </ul>
17 </body>
18 </html>




Logo
作者:Gezidan
出处:http://www.rixu.net    
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
本文转载自 
http://www.cnblogs.com/javennie/archive/2011/09/20/xslt.html
posted on 2011-09-28 10:27 日需博客 阅读(401) 评论(0)  编辑 收藏 引用 所属分类: C#技术文章转载

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理