<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Khanhpt&#039;s Weblog</title>
	<atom:link href="http://khanhpt.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://khanhpt.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Tue, 14 Jun 2011 02:51:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='khanhpt.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Khanhpt&#039;s Weblog</title>
		<link>http://khanhpt.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://khanhpt.wordpress.com/osd.xml" title="Khanhpt&#039;s Weblog" />
	<atom:link rel='hub' href='http://khanhpt.wordpress.com/?pushpress=hub'/>
		<item>
		<title>The difference between &lt;%= and &lt;%# in ASP.NET</title>
		<link>http://khanhpt.wordpress.com/2011/06/14/the-difference-between-and-in-asp-net/</link>
		<comments>http://khanhpt.wordpress.com/2011/06/14/the-difference-between-and-in-asp-net/#comments</comments>
		<pubDate>Tue, 14 Jun 2011 02:37:33 +0000</pubDate>
		<dc:creator>khanhpt</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Asp.Net with C#]]></category>

		<guid isPermaLink="false">http://khanhpt.wordpress.com/?p=67</guid>
		<description><![CDATA[I was a little confused about the difference between &#60;%= expression %&#62; and &#60;%# expression %&#62; in ASP.NET. It seems like both work in a lot of cases, but in other cases, only the # (data binding) version works. So, I decided to dig into it a little bit. To try it out I built [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=khanhpt.wordpress.com&amp;blog=4686976&amp;post=67&amp;subd=khanhpt&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div>
<p>I was a little confused about the difference between &lt;%= <em>expression</em> %&gt; and &lt;%# <em>expression</em> %&gt; in ASP.NET. It seems like both work in a lot of cases, but in other cases, only the # (data binding) version works. So, I decided to dig into it a little bit. To try it out I built this simple page:</p>
<pre><span style="color:#ff0000;">&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %&gt;</span>

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

&lt;html xmlns="http://www.w3.org/1999/xhtml" &gt;
&lt;head runat="server"&gt;
    &lt;title&gt;Untitled Page&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;form id="form1" runat="server"&gt;
    &lt;div&gt;
        &lt;p&gt;Equals: &lt;%= this.TestValue %&gt;&lt;/p&gt;
        &lt;p&gt;Pound: &lt;%# this.TestValue %&gt;&lt;/p&gt;
        &lt;p&gt;Equals label: &lt;asp:Label runat="server" ID="_equals" Text="&lt;%= this.TestValue %&gt;" /&gt;&lt;/p&gt;
        &lt;p&gt;Pound label: &lt;asp:Label runat="server" ID="_pound" Text="&lt;%# this.TestValue %&gt;" /&gt;&lt;/p&gt;
    &lt;/div&gt;
    &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<p>And the code behind is:</p>
<pre>public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        _testValue = "2";
    }

    protected void Page_PreRenderComplete(object sender, EventArgs e)
    {
        // DataBind();
        _testValue = "3";
    }

    public string TestValue
    {
        get { return _testValue; }
    }

    private string _testValue = "1";
}</pre>
<p>Here&#8217;s what the result is when the DataBind() line is commented out:</p>
<blockquote><p>Equals: 3</p>
<p>Pound:</p>
<p>Equals label:</p>
<p>Pound label:</p></blockquote>
<p>And, when it&#8217;s not commented out:</p>
<blockquote><p>Equals: 3</p>
<p>Pound: 2</p>
<p>Equals label:</p>
<p>Pound label: 2</p></blockquote>
<p>At first glance it looks like the Equals label case did nothing. But, if you view source, you see:</p>
<blockquote><p>&lt;p&gt;Equals label: &lt;span id=&#8221;_equals&#8221;&gt;&lt;%= this.TestValue %&gt;&lt;/span&gt;&lt;/p&gt;</p></blockquote>
<p>The literal expression made it down to the browser and it&#8217;s just invalid HTML. What you can see as a result is:</p>
<ul>
<li><strong><span style="color:#ff0000;">The &lt;%= expressions are evaluated at render time</span></strong></li>
<li><strong><span style="color:#ff0000;">The &lt;%# expressions are evaluated at DataBind() time and are not evaluated at all if DataBind() is not called.</span></strong></li>
<li><strong><span style="color:#ff0000;">&lt;%# expressions can be used as properties in server-side controls. &lt;%= expressions cannot.</span></strong></li>
</ul>
<p>Now, let&#8217;s look at the generated code to see how it works. It builds the control tree using the following code:</p>
<pre>        private void @__BuildControlTree(default_aspx @__ctrl) {
            this.InitializeCulture();

            System.Web.UI.IParserAccessor @__parser = ((System.Web.UI.IParserAccessor)(@__ctrl));
            @__parser.AddParsedSubObject(new
                        System.Web.UI.LiteralControl("\r\n\r\n&lt;!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3" +
                        ".org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"&gt;\r\n\r\n&lt;html xmlns=\"http://www.w3.org/1" +
                        "999/xhtml\" &gt;\r\n"));

            global::System.Web.UI.HtmlControls.HtmlHead @__ctrl1;
            @__ctrl1 = this.@__BuildControl__control2();
            @__parser.AddParsedSubObject(@__ctrl1);
            @__parser.AddParsedSubObject(new System.Web.UI.LiteralControl("\r\n&lt;body&gt;\r\n    "));

            global::System.Web.UI.HtmlControls.HtmlForm @__ctrl2;
            @__ctrl2 = this.@__BuildControlform1();
            @__parser.AddParsedSubObject(@__ctrl2);

            @__parser.AddParsedSubObject(new System.Web.UI.LiteralControl("\r\n&lt;/body&gt;\r\n&lt;/html&gt;\r\n"));
        }</pre>
<p>@__BuildControl__control2() isn&#8217;t that intersting, it&#8217;s just the &lt;head&gt; stuff. The guts are in @__BuildControlform1():</p>
<pre>        private global::System.Web.UI.HtmlControls.HtmlForm @__BuildControlform1() {
            global::System.Web.UI.HtmlControls.HtmlForm @__ctrl;
            @__ctrl = new global::System.Web.UI.HtmlControls.HtmlForm();
            this.form1 = @__ctrl;
            @__ctrl.ID = "form1";

            global::System.Web.UI.DataBoundLiteralControl @__ctrl1;
            @__ctrl1 = this.@__BuildControl__control4();
            System.Web.UI.IParserAccessor @__parser = ((System.Web.UI.IParserAccessor)(@__ctrl));
            @__parser.AddParsedSubObject(@__ctrl1);

            global::System.Web.UI.WebControls.Label @__ctrl2;
            @__ctrl2 = this.@__BuildControl_equals();
            @__parser.AddParsedSubObject(@__ctrl2);

            global::System.Web.UI.WebControls.Label @__ctrl3;
            @__ctrl3 = this.@__BuildControl_pound();
            @__parser.AddParsedSubObject(@__ctrl3);

            @__ctrl.SetRenderMethodDelegate(new System.Web.UI.RenderMethod(this.@__Renderform1));
            return @__ctrl;
        }</pre>
<p>This is building all of the controls in the form. First let&#8217;s look at the control built with @__BuildControl__control4():</p>
<pre>        private global::System.Web.UI.DataBoundLiteralControl @__BuildControl__control4() {
            global::System.Web.UI.DataBoundLiteralControl @__ctrl;

            @__ctrl = new global::System.Web.UI.DataBoundLiteralControl(2, 1);
            @__ctrl.SetStaticString(0, "&lt;/p&gt;\r\n        &lt;p&gt;Pound: ");
            @__ctrl.SetStaticString(1, "&lt;/p&gt;\r\n        &lt;p&gt;Equals label: ");
            @__ctrl.DataBinding += new System.EventHandler(this.@__DataBind__control4);

            return @__ctrl;
        }</pre>
<p>And related to this is:</p>
<pre>        public void @__DataBind__control4(object sender, System.EventArgs e) {
            System.Web.UI.Page Container;
            System.Web.UI.DataBoundLiteralControl target;

            target = ((System.Web.UI.DataBoundLiteralControl)(sender));
            Container = ((System.Web.UI.Page)(target.BindingContainer));
            target.SetDataBoundString(0, System.Convert.ToString(this.TestValue, System.Globalization.CultureInfo.CurrentCulture));
        }</pre>
<p>This is the part of the page up to the first &lt;%#. You can see that it uses a DataBoundLiteralControl which is divided between static strings and data bound strings. When DataBind is called on the page, the event handler is called and SetDataBoundString is called on the control. This is why the value is captured at bind time. If you want to see how DataBoundLiteralControl works, you can use .NET Reflector. You can see that it&#8217;s Render function looks like:</p>
<pre>    protected internal override void Render(HtmlTextWriter output)
    {
        int num1 = this._dataBoundLiteral.Length;
        for (int num2 = 0; num2 &lt; this._staticLiterals.Length; num2++)
        {
            if (this._staticLiterals[num2] != null)
            {
                output.Write(this._staticLiterals[num2]);
            }
            if ((num2 &lt; num1) &amp;&amp; (this._dataBoundLiteral[num2] != null))
            {
                output.Write(this._dataBoundLiteral[num2]);
            }
        }
    }</pre>
<p>So, it basically outputs the static and data bound fields alternately. That&#8217;s how it renders the first &lt;%# value on the page if DataBind() is called. Next, let&#8217;s look at the &#8220;_equals&#8221; control:</p>
<pre>        private global::System.Web.UI.WebControls.Label @__BuildControl_equals() {
            global::System.Web.UI.WebControls.Label @__ctrl;

            @__ctrl = new global::System.Web.UI.WebControls.Label();
            @__ctrl.ApplyStyleSheetSkin(this);
            @__ctrl.ID = "_equals";
            @__ctrl.Text = "&lt;%= this.TestValue %&gt;";

            return @__ctrl;
        }</pre>
<p>You can see that the &lt;%= is not in any way special. It&#8217;s just literal text. The &#8220;_pound&#8221; control, which uses &lt;%# on the other hand, looks like:</p>
<pre>        private global::System.Web.UI.WebControls.Label @__BuildControl_pound() {
            global::System.Web.UI.WebControls.Label @__ctrl;

            @__ctrl = new global::System.Web.UI.WebControls.Label();
            @__ctrl.ApplyStyleSheetSkin(this);
            @__ctrl.ID = "_pound";
            @__ctrl.DataBinding += new System.EventHandler(this.@__DataBinding_pound);

            return @__ctrl;
        }

        public void @__DataBinding_pound(object sender, System.EventArgs e) {
            System.Web.UI.WebControls.Label dataBindingExpressionBuilderTarget;
            System.Web.UI.Page Container;
            dataBindingExpressionBuilderTarget = ((System.Web.UI.WebControls.Label)(sender));
            Container = ((System.Web.UI.Page)(dataBindingExpressionBuilderTarget.BindingContainer));

            dataBindingExpressionBuilderTarget.Text = System.Convert.ToString( this.TestValue , System.Globalization.CultureInfo.CurrentCulture);
        }</pre>
<p>As compared to the &lt;%= version, &lt;%# is treated specially in parameters. It adds a data binding handler and sets the property at data bind time. That&#8217;s why &lt;%# does work in parameters and why it picks up the value at data bind time.</p>
<p>So, how does the first &lt;%= work? We can see this by looking at the following function:</p>
<pre>        private void @__Renderform1(System.Web.UI.HtmlTextWriter @__w, System.Web.UI.Control parameterContainer) {
            @__w.Write("        &lt;p&gt;Equals: ");
            @__w.Write( this.TestValue );

            parameterContainer.Controls[0].RenderControl(@__w);
            parameterContainer.Controls[1].RenderControl(@__w);
            @__w.Write("&lt;/p&gt;\r\n        &lt;p&gt;Pound label: ");
            parameterContainer.Controls[2].RenderControl(@__w);
            @__w.Write("&lt;/p&gt;\r\n    &lt;/div&gt;\r\n    ");
        }</pre>
<p>Here we can see the @__w.Write(this.TestValue) line which is the result of the &lt;%= line in the source. It&#8217;s outputting the value of this.TestValue at render time, which explains the behavior we saw.</p>
<p>So, it all makes sense to me now. I hope it makes sense to you too <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Original Post : http://blogs.msdn.com/b/dancre/archive/2007/02/13/the-difference-between-lt-and-lt-in-asp-net.aspx</p>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/khanhpt.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/khanhpt.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/khanhpt.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/khanhpt.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/khanhpt.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/khanhpt.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/khanhpt.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/khanhpt.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/khanhpt.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/khanhpt.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/khanhpt.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/khanhpt.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/khanhpt.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/khanhpt.wordpress.com/67/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=khanhpt.wordpress.com&amp;blog=4686976&amp;post=67&amp;subd=khanhpt&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://khanhpt.wordpress.com/2011/06/14/the-difference-between-and-in-asp-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/72008dc052f08cf83608b61df719edc9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">khanhpt</media:title>
		</media:content>
	</item>
		<item>
		<title>Lấy và Ngày tháng trong SQL</title>
		<link>http://khanhpt.wordpress.com/2009/07/16/l%e1%ba%a5y-va-ngay-thang-trong-sql/</link>
		<comments>http://khanhpt.wordpress.com/2009/07/16/l%e1%ba%a5y-va-ngay-thang-trong-sql/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 02:20:10 +0000</pubDate>
		<dc:creator>khanhpt</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[ngày tháng]]></category>
		<category><![CDATA[Sql]]></category>

		<guid isPermaLink="false">http://khanhpt.wordpress.com/2009/07/16/l%e1%ba%a5y-va-ngay-thang-trong-sql/</guid>
		<description><![CDATA[Thông thường khi lưu dữ liệu vào SQL ta thường lưu dưới dạng Ngày tháng kèm theo thời gian đầy đủ. Trong một số trường hợp chỉ cần lấy Ngày Tháng Năm trong cột dữ liệu này để so sánh thì chỉ cần dùng hàm được xây dựng sẵn trong SQL như sau SELECT CONVERT(VARCHAR(10),GETDATE(),103) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=khanhpt.wordpress.com&amp;blog=4686976&amp;post=60&amp;subd=khanhpt&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Thông thường khi lưu dữ liệu vào SQL ta thường lưu dưới dạng Ngày tháng kèm theo thời gian đầy đủ. Trong một số trường hợp chỉ cần lấy Ngày Tháng Năm trong cột dữ liệu này để so sánh thì chỉ cần dùng hàm được xây dựng sẵn trong SQL như sau</p>
<blockquote><p><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">),</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">(),103</span><span style="color:gray;">)</span></code></p></blockquote>
<blockquote></blockquote>
<blockquote><p>Với  GetDate() là hàm lấy ngày tháng trong SQL, bạn có thể thay thế bằng cột chứa ngày tháng của bạn trong table vd :</p></blockquote>
<blockquote>
<blockquote><p>SELECT   (CONVERT(VARCHAR(10),NgayThangCollum,103)    FROM    YourTable</p></blockquote>
</blockquote>
<p>Tham số 103 là kiểu ngày tháng theo từng quốc gia, 103 ở trên là kiểu ngày tháng dd/mm/yy mà Việt Nam mình đang dùng <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Trên đây chỉ đơn giản là cách lấy ngày tháng không kèm thời gian, để lấy dữ liệu theo một ngày tháng nhất định nào đó bạn có thể dùng sql query như sau :</p>
<blockquote><p>SELECT    *    FROM    yourTable</p>
<p>where ((CONVERT(VARCHAR(10),VisitDate ,103)) like  &#8216;dd/mm/yy&#8217;)</p></blockquote>
<p>Hoặc dùng biến ngày tháng kiểu string dạng short date string</p>
<blockquote><p>@YourDate varchar(10)</p></blockquote>
<blockquote><p>SELECT    *    FROM    yourTable</p>
<p>where ((CONVERT(VARCHAR(10),VisitDate ,103)) like  @YourDate)</p></blockquote>
<p>Chúc thành công <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>link tham khảo :</p>
<p>http://blog.sqlauthority.com/2007/06/10/sql-server-retrieve-select-only-date-part-from-datetime-best-practice/</p>
<p>http://msdn.microsoft.com/en-us/library/ms187928.aspx</p>
<blockquote></blockquote>
<blockquote><p><span style="color:gray;"><br />
</span></p>
<blockquote><p><code style="font-size:12px;"><span style="color:blue;"><br />
</span><span style="color:gray;"></span></code></p>
<p><span style="color:gray;"><br />
</span></p></blockquote>
</blockquote>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/khanhpt.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/khanhpt.wordpress.com/60/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/khanhpt.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/khanhpt.wordpress.com/60/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/khanhpt.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/khanhpt.wordpress.com/60/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/khanhpt.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/khanhpt.wordpress.com/60/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/khanhpt.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/khanhpt.wordpress.com/60/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/khanhpt.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/khanhpt.wordpress.com/60/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/khanhpt.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/khanhpt.wordpress.com/60/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=khanhpt.wordpress.com&amp;blog=4686976&amp;post=60&amp;subd=khanhpt&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://khanhpt.wordpress.com/2009/07/16/l%e1%ba%a5y-va-ngay-thang-trong-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/72008dc052f08cf83608b61df719edc9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">khanhpt</media:title>
		</media:content>
	</item>
		<item>
		<title>Check UserRoles in UserControl</title>
		<link>http://khanhpt.wordpress.com/2009/05/08/check-userroles-in-usercontrol/</link>
		<comments>http://khanhpt.wordpress.com/2009/05/08/check-userroles-in-usercontrol/#comments</comments>
		<pubDate>Fri, 08 May 2009 03:50:46 +0000</pubDate>
		<dc:creator>khanhpt</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Asp.Net with C#]]></category>

		<guid isPermaLink="false">http://khanhpt.wordpress.com/?p=58</guid>
		<description><![CDATA[using System.Security.Principal; Code : IPrincipal user = this.Page.User; if (User.IsInRole(&#8220;administrator&#8221;)) { // do anything }<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=khanhpt.wordpress.com&amp;blog=4686976&amp;post=58&amp;subd=khanhpt&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<blockquote><p>using System.Security.Principal;</p>
<p>Code :</p>
<p>IPrincipal user = this.Page.User;</p>
<p>if (User.IsInRole(&#8220;administrator&#8221;))</p>
<p>{</p>
<p>// do anything</p>
<p>}</p></blockquote>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/khanhpt.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/khanhpt.wordpress.com/58/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/khanhpt.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/khanhpt.wordpress.com/58/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/khanhpt.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/khanhpt.wordpress.com/58/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/khanhpt.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/khanhpt.wordpress.com/58/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/khanhpt.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/khanhpt.wordpress.com/58/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/khanhpt.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/khanhpt.wordpress.com/58/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/khanhpt.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/khanhpt.wordpress.com/58/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=khanhpt.wordpress.com&amp;blog=4686976&amp;post=58&amp;subd=khanhpt&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://khanhpt.wordpress.com/2009/05/08/check-userroles-in-usercontrol/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/72008dc052f08cf83608b61df719edc9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">khanhpt</media:title>
		</media:content>
	</item>
		<item>
		<title>get all documents in the index using Lucence</title>
		<link>http://khanhpt.wordpress.com/2009/04/02/get-all-documents-in-the-index-using-lucence/</link>
		<comments>http://khanhpt.wordpress.com/2009/04/02/get-all-documents-in-the-index-using-lucence/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 08:55:10 +0000</pubDate>
		<dc:creator>khanhpt</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[document]]></category>
		<category><![CDATA[get all]]></category>
		<category><![CDATA[lucence]]></category>

		<guid isPermaLink="false">http://khanhpt.wordpress.com/?p=55</guid>
		<description><![CDATA[IndexReader r = IndexReader.Open(path-to-index-folder); DataTable dt = new DataTable(); dt.Columns.Add(IndexedFields.ID, typeof(string)); int num = r.NumDocs(); for (int i = 0; i &#60; num; i++) { if (!r.IsDeleted(i)) { Document d = r.Document(i); DataRow row = dt.NewRow(); row[IndexedFields.ID] = d.Get(IndexedFields.ID); dt.Rows.Add(row); } } r.Close(); foreach (DataRow dr in dt.Rows) { string id = dr["ID"].ToString(); //do what [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=khanhpt.wordpress.com&amp;blog=4686976&amp;post=55&amp;subd=khanhpt&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>IndexReader r = IndexReader.Open(path-to-index-folder);<br />
DataTable dt = new DataTable();<br />
dt.Columns.Add(IndexedFields.ID, typeof(string));<br />
int num = r.NumDocs();<br />
for (int i = 0; i &lt; num; i++)<br />
{<br />
if (!r.IsDeleted(i))<br />
{<br />
Document d = r.Document(i);<br />
DataRow row = dt.NewRow();<br />
row[IndexedFields.ID] = d.Get(IndexedFields.ID);<br />
dt.Rows.Add(row);<br />
}<br />
}<br />
r.Close();</p>
<p>foreach (DataRow dr in dt.Rows)<br />
{<br />
string id = dr["ID"].ToString();<br />
//do what you want<br />
}</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/khanhpt.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/khanhpt.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/khanhpt.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/khanhpt.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/khanhpt.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/khanhpt.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/khanhpt.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/khanhpt.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/khanhpt.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/khanhpt.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/khanhpt.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/khanhpt.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/khanhpt.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/khanhpt.wordpress.com/55/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=khanhpt.wordpress.com&amp;blog=4686976&amp;post=55&amp;subd=khanhpt&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://khanhpt.wordpress.com/2009/04/02/get-all-documents-in-the-index-using-lucence/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/72008dc052f08cf83608b61df719edc9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">khanhpt</media:title>
		</media:content>
	</item>
		<item>
		<title>Full-Text Search in ASP.NET using Lucene.NET</title>
		<link>http://khanhpt.wordpress.com/2009/04/02/full-text-search-in-aspnet-using-lucenenet/</link>
		<comments>http://khanhpt.wordpress.com/2009/04/02/full-text-search-in-aspnet-using-lucenenet/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 08:08:06 +0000</pubDate>
		<dc:creator>khanhpt</dc:creator>
				<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">http://khanhpt.wordpress.com/2009/04/02/full-text-search-in-aspnet-using-lucenenet/</guid>
		<description><![CDATA[This post is about the full-text search engine Lucene.NET and how I integrated it into BugTracker.NET .   If you are thinking of adding full-text search to your application, you might find this post useful.  I&#8217;m not saying this is THE way of using Lucene.NET, but it is an example of ONE way. Lucene.NET is a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=khanhpt.wordpress.com&amp;blog=4686976&amp;post=53&amp;subd=khanhpt&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div>
<div>This post is about the full-text search engine <a id="h87j" title="Lucene.NET" href="http://incubator.apache.org/lucene.net/">Lucene.NET</a> and how I integrated it into <a id="n27n" title="BugTracker.NET" href="http://www.ifdefined.com/bugtrackernet.html">BugTracker.NET</a> .   If you are thinking of adding full-text search to your application, you might find this post useful.  I&#8217;m not saying this is THE way of using Lucene.NET, but it is an example of ONE way.</div>
<div></div>
<div>Lucene.NET is a C# port of the original <a id="i7ac" title="Lucene" href="http://lucene.apache.org/java/docs/">Lucene</a> , an Apache Foundation open source project, written in java.</p>
<p>Why did I use Lucene.NET instead of the SQL Server full-text search engine?  Well, I&#8217;d like to say that I did some research into the pros and cons of the two choices, but actually I didn&#8217;t do any comparative research.  What happened was that during a <a id="r7vb" title="Stackoverflow podcast" href="http://itc.conversationsnetwork.org/shows/detail3712.html">Stackoverflow podcast</a> I heard Joel Spolsky mention that FogBugz uses Lucene as its engine and that he was happy with it.   I trust him, and  I was curious, so, one weekend I downloaded Lucene.NET and played with it a bit and before the weekend was over I was already done integrating it into BugTracker.NET.   I never looked at the SQL Server alternative at all, so I can&#8217;t tell you anything about it.</p>
<p>Lucene itself is a class library, not an executable.   You call Lucene functions to do the search.  There is an open source standalone server built on Lucene called <a id="tuwy" title="Solr" href="http://lucene.apache.org/solr/">Solr</a> .   You send Solr messages to do the search.  One way of using Lucene would have been to have my users run Solr side-by-side with SQL Server.   As with SQL Server full-text search, I can&#8217;t tell you anything about Solr because I didn&#8217;t try it.   It wouldn&#8217;t have made sense to use Solr for BugTracker.NET, I think, because Solr would have been an additional installation hassle.   And running a server wouldn&#8217;t have been doable at all at a cheap shared host like GoDaddy, where my own BugTracker.NET demo lives.    So, instead of using Solr, I used the Lucene class libraries directly.</p>
<p>To integrate Lucene, I had to build the following, which I list here and then describe in more detail below.</p></div>
<div>
<div style="margin-left:40px;"><strong>1) How Lucene would build its searchable index. </strong>Lucene doesn&#8217;t search my SQL Server database directly.   Instead, it searches its own &#8220;database&#8221;, its own index.<br />
<strong>2) The design of the Lucene index</strong><br />
<strong>3) How I would update Lucene&#8217;s index whenever data in my database changes.</strong><br />
<strong>4) Sending the search query to Lucene.</strong><br />
<strong>5) Displaying the results.</strong></div>
<p>Now the details.  I&#8217;ve simplied my code for this post, so that you can more easily see the overall design and understand the concepts and my design choices.</p>
<p><strong>1) Building the index.</strong></p>
<p>When an ASP.NET application receives its first HTTP request after having been shut down, the Application_OnStart event fires, which I handle in Global.asax.   I call my &#8220;build_lucene_index&#8221; method.    Notice that I have a configuration setting &#8220;EnableLucene&#8221;.    I was nervous about the my understanding of Lucene and whether my way of using it was the right architecture, and so I wanted to make sure I gave my users a way of turning Lucene off in case it was causing trouble.    More on that in a bit.</p>
<p>For a really big database, you wouldn&#8217;t want to necessarily build the search index from scratch over and over, but I&#8217;m counting on BugTracker.NET databases being on the small side.   Is that a safe assumption?  A bug database shouldn&#8217;t be that big or else you&#8217;re doing it wrong, right?</p>
<div style="margin-left:40px;"><span style="color:#000000;">public void <span style="background-color:#ffff00;">Application_OnStart</span>(Object sender, EventArgs e)</span><br />
<span style="color:#000000;">{</span><br />
<span style="color:#000000;"> if (btnet.Util.get_setting(&#8220;EnableLucene&#8221;, &#8221;1&#8243;) == &#8221;1&#8243;)</span><br />
<span style="color:#000000;"> {</span><br />
<span style="color:#000000;"> <span style="background-color:#ffff00;">build_lucene_index</span>(this.Application);</span><br />
<span style="color:#000000;"> }</span><br />
<span style="color:#000000;">}</span></div>
</div>
</div>
<div>The build_lucene_index method starts a new worker thread, where the real work is done.</p>
<div style="margin-left:40px;"><span style="color:#000000;">public static void <span style="background-color:#ffff00;">build_lucene_index</span>(System.Web.HttpApplicationState app)</span><br />
<span style="color:#000000;">{</span><br />
<span style="color:#000000;"> System.Threading.Thread thread = new System.Threading.Thread(<span style="background-color:#ffff00;">threadproc_build</span>);</span><br />
<span style="color:#000000;"> <span style="background-color:#ffff00;">thread.Start</span>(app);</span><br />
<span style="color:#000000;">}</span></div>
<p>The worker thread first grabs a lock so that it can build the index without being disturbed by other threads.   The other threads would be the result of users either searching or users updating text, triggering a modification to Lucene&#8217;s index.    I don&#8217;t want those threads to be dealing with a partially built index, so I make those threads wait for the one-and-only lock.</p>
<p>My way of handling multithreading was one of the things that I was nervous about.   I feared some sort of hard-to-reproduce deadlock condition, or race condition, but so far, there have been no reports from BugTracker.NET users of any trouble, so I my design appears to be solid.</p>
<p>To create the index, I create a Lucene &#8220;IndexWriter&#8221;.   I run a SQL query against my database to fetch the text I want to be able to search and the database keys that go with that text.   Then I loop through the query results adding a Lucene &#8220;Document&#8221; for each row.   Actually, in my real code, I get the searchable text from several different fields in my database, but in the snippet below I have simplified my harvesting of text from my database.</p>
<div style="margin-left:40px;"><span style="color:#000000;">Lucene.Net.Analysis.Standard.StandardAnalyzer analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer();</span></p>
<p><span style="color:#000000;">static void <span style="background-color:#ffff00;">threadproc_build</span>(object obj)</span><br />
<span style="color:#000000;">{</span><br />
<span style="color:#000000;"> <span style="background-color:#ffff00;">lock (my_lock)</span></span><br />
<span style="color:#000000;"> {</span><br />
<span style="color:#000000;"> try</span><br />
<span style="color:#000000;"> {</span></p>
<p><span style="color:#000000;"> Lucene.Net.Index.IndexWriter writer = <span style="background-color:#ffff00;">new Lucene.Net.Index.IndexWriter(&#8220;c:\\folder_where_lucene_index_lives&#8221;, analyzer , true);</span></span><br />
<span style="color:#000000;"> </span><br />
<span style="color:#000000;"> DataSet ds = btnet.DbUtil.get_dataset(<span style="background-color:#ffff00;">&#8220;select bug_id, bug_text from bugs&#8221;</span>)</span><br />
<span style="color:#000000;"> </span><br />
<span style="color:#000000;"> <span style="background-color:#ffff00;">foreach (DataRow dr in ds.Tables[0].Rows</span>)</span><br />
<span style="color:#000000;"> {</span><br />
<span style="color:#000000;"> <span style="background-color:#ffff00;">writer.AddDocument</span>(<span style="background-color:#ffff00;">create_doc</span>(</span><br />
<span style="color:#000000;"> (int)dr["bug_id"],</span><br />
<span style="color:#000000;"> (string)dr["bug_text"]));</span><br />
<span style="color:#000000;"> }</span><br />
<span style="color:#000000;"> </span><br />
<span style="color:#000000;"> writer.<span style="background-color:#ffff00;">Optimize</span>();</span><br />
<span style="color:#000000;"> writer.Close();</span><br />
<span style="color:#000000;"> }</span><br />
<span style="color:#000000;"> catch (Exception e)</span><br />
<span style="color:#000000;"> {</span><br />
<span style="color:#000000;"> btnet.Util.write_to_log(&#8220;exception building Lucene index: &#8221; + e.Message);</span><br />
<span style="color:#000000;"> }</span><br />
<span style="color:#000000;"> }</span><br />
<span style="color:#000000;">}</span></div>
<div style="margin-left:40px;"></div>
<p><span style="color:#000000;"></p>
<p></span><strong>2) The design of the Lucene Index</strong></p>
<p>Here&#8217;s where I create a Lucene &#8220;Document&#8221;.    An index contains a list of documents.   A doc has fields that you define.   My doc shown here has three fields.   The first field &#8220;text&#8221; is what Lucene will analyze and index, the searchable text.  The second field is the key I will use to link the Lucene data to the rows in my database.   Notice I tell Lucene that this key should be UN_TOKENIZED, stored as is.   That&#8217;s all you need for a minimal Lucene doc, a key for you and some text to search on for Lucene.  The third field in my example is the text again, but this time, UN_TOKENIZED, stored as is.  I will use that text for having Lucene highlight in my results page the snippets where the hits are.   More on highlighting later.</p>
<p>One of the decisions you&#8217;ll have to make when using Lucene is what text to index and how to package it for Lucene.    In my database, the text doesn&#8217;t just live in one field.    A bug has a short text description, a list of comments, a list of incoming and outgoing emails, and even Digg-style tags.   In my real code as opposed to the snippets here,  I fetch text from all these places.   My real Lucene doc has four fields, the forth being another database key that I can use to link to the specific comment or email where the search hit is.   BugTracker.NET supports custom text fields and in the future I hope to harvest that text from the database and add it to the Lucene doc.</p>
<p>So, if your app is like mine, with text in many different places, then you&#8217;ll have a challenge like mine, how to package the text into a Lucene doc.</p>
<div style="margin-left:40px;"><span style="color:#000000;">static Lucene.Net.Documents.Document <span style="background-color:#ffff00;">create_doc</span>(int bug_id, string text)</span><br />
<span style="color:#000000;">{</span><span style="color:#000000;"> </span><br />
<span style="color:#000000;"> Lucene.Net.Documents.Document doc = <span style="background-color:#ffff00;">new Lucene.Net.Documents.Document();</span></span><br />
<span style="color:#000000;"> </span><br />
<span style="color:#000000;"> <span style="background-color:#ffff00;">doc.Add(new Lucene.Net.Documents.Field(</span></span><br />
<span style="color:#000000;"> &#8220;text&#8221;,</span><br />
<span style="color:#000000;"> new System.IO.StringReader(<span style="background-color:#ffff00;">text</span>)));</span><br />
<span style="color:#000000;"> </span><br />
<span style="color:#000000;"> <span style="background-color:#ffff00;">doc.Add(new Lucene.Net.Documents.Field(</span></span><br />
<span style="color:#000000;"> &#8220;bug_id&#8221;,</span><br />
<span style="color:#000000;"> Convert.ToString(<span style="background-color:#ffff00;">bug_id</span>),</span><br />
<span style="color:#000000;"> Lucene.Net.Documents.Field.Store.YES,</span><br />
<span style="color:#000000;"> Lucene.Net.Documents.Field.Index.<span style="background-color:#ffff00;">UN_TOKENIZED</span>));</span><br />
<span style="color:#000000;"> </span><br />
<span style="color:#000000;"> // For the highlighter, store the raw text</span><br />
<span style="color:#000000;"> <span style="background-color:#ffff00;">doc.Add(new Lucene.Net.Documents.Field(</span></span><br />
<span style="color:#000000;"> &#8220;raw_text&#8221;,</span><br />
<span style="color:#000000;"> <span style="background-color:#ffff00;">text</span>,</span><br />
<span style="color:#000000;"> Lucene.Net.Documents.Field.Store.YES,</span><br />
<span style="color:#000000;"> Lucene.Net.Documents.Field.Index.<span style="background-color:#ffff00;">UN_TOKENIZED</span>));</span></p>
<p><span style="color:#000000;"> return doc;</span><br />
<span style="color:#000000;">}</span></div>
<p><strong>3) Updating the index</p>
<p></strong>Whenever a user updates text in a bug I launch a worker thread to update the index.    The worker thread grabs a lock so that only one thread is updating the index at a time.</p>
<p>The worker thread creates a Lucene &#8220;IndexModifier&#8221;, deletes the old doc, and replaces it with a new one.</p>
<p>Notice that the thread closes the &#8220;searcher&#8221;.   The searcher is a Lucene &#8220;Searcher&#8221;.   The life cycle of a Searcher is that it first loads the index and then does its searches using that loaded, cached version of the index.   If the real index changes on disk, the searcher wouldn&#8217;t know about it.   It would continue searching the out-of-date cached copy of the index in its memory.    That might be ok for your situation, and if your index is very big and the cost of creating a new searcher is high, you might be forced to use a searcher with a stale index.   BugTracker.NET databases tend to be small, so I can get away with making sure my searcher always has an up-to-date index to work with.</p>
<p>The official Lucene fact says that a Searcher (aka IndexSearcher) &#8220;is thread-safe. Multiple search threads may use the same instance of IndexSearcher concurrently without any problems. It is recommended to use only one IndexSearcher from all threads in order to save memory.&#8221;</p>
<div style="margin-left:40px;"><span style="color:#000000;">Lucene.Net.Search.Searcher <span style="background-color:#ffff00;">searcher</span> = null;</span></p>
<p><span style="color:#000000;">static void threadproc_update(object obj)</span><br />
<span style="color:#000000;">{</span><br />
<span style="color:#000000;"> <span style="background-color:#ffff00;">lock (my_lock)</span> // If a thread is updating the index, no other thread should be doing anything with it.</span><br />
<span style="color:#000000;"> {</span><br />
<span style="color:#000000;"> </span><br />
<span style="color:#000000;"> try</span><br />
<span style="color:#000000;"> {</span><br />
<span style="color:#000000;"> if (searcher != null)</span><br />
<span style="color:#000000;"> {</span><br />
<span style="color:#000000;"> try</span><br />
<span style="color:#000000;"> {</span><br />
<span style="color:#000000;"> searcher.Close();</span><br />
<span style="color:#000000;"> }</span><br />
<span style="color:#000000;"> catch (Exception e)</span><br />
<span style="color:#000000;"> {</span><br />
<span style="color:#000000;"> btnet.Util.write_to_log(&#8220;Exception closing lucene searcher:&#8221; + e.Message);</span><br />
<span style="color:#000000;"> }</span><br />
<span style="color:#000000;"> searcher = null;</span><br />
<span style="color:#000000;"> }</span><br />
<span style="color:#000000;"> </span><br />
<span style="color:#000000;"> Lucene.Net.Index.IndexModifier modifier = <span style="background-color:#ffff00;">new Lucene.Net.Index.IndexModifier(&#8220;c:\\folder_where_lucene_index_lives&#8221;, analyzer, false);</span></span><br />
<span style="color:#000000;"> </span><br />
<span style="color:#000000;"> // same as build, but uses &#8221;modifier&#8221; instead of write.</span><br />
<span style="color:#000000;"> // uses additional &#8221;where&#8221; clause for bugid</span><br />
<span style="color:#000000;"> </span><br />
<span style="color:#000000;"> int bug_id = (int)obj;</span><br />
<span style="color:#000000;"> </span><br />
<span style="color:#000000;"> modifier.<span style="background-color:#ffff00;">DeleteDocuments</span>(new Lucene.Net.Index.Term(&#8220;bug_id&#8221;, Convert.ToString(bug_id)));</span><br />
<span style="color:#000000;"> </span><br />
<span style="color:#000000;"> DataSet ds = btnet.DbUtil.get_dataset(&#8220;select bug_id, bug_text from bugs where bug_id = &#8221; + ConvertToString(bug_id));</span><br />
<span style="color:#000000;"> </span><br />
<span style="color:#000000;"> foreach (DataRow dr in ds.Tables[0].Rows) // one row&#8230;</span><br />
<span style="color:#000000;"> {</span><br />
<span style="color:#000000;"> modifier.<span style="background-color:#ffff00;">AddDocument</span>(create_doc(</span><br />
<span style="color:#000000;"> (int)dr["bug_id"],</span><br />
<span style="color:#000000;"> (string)dr["bug_text"]));</span><br />
<span style="color:#000000;"> }</span><br />
<span style="color:#000000;"> </span><br />
<span style="color:#000000;"> modifier.<span style="background-color:#ffff00;">Flush</span>();</span><br />
<span style="color:#000000;"> modifier.Close();</span><br />
<span style="color:#000000;"> </span><br />
<span style="color:#000000;"> }</span><br />
<span style="color:#000000;"> catch (Exception e)</span><br />
<span style="color:#000000;"> {</span><br />
<span style="color:#000000;"> btnet.Util.write_to_log(&#8220;exception updating Lucene index: &#8221; + e.Message);</span><br />
<span style="color:#000000;"> }</span><br />
<span style="color:#000000;"> }</span><br />
<span style="color:#000000;">}</span></div>
<p><strong>4) Sending the search query to Lucene<br />
</strong><br />
To search, create a Lucene &#8220;QueryParser&#8221;.    Call its Parse() method passing the text the user typed in.   The Parse() method returns a &#8220;Query&#8221;.   Call the Searcher&#8217;s Search() method passing the Query.   The Search() method returns a Lucene &#8220;Hits&#8221; object, a collection of the search hits.</p>
<p>As I&#8217;ve mentioned, I want my searcher to always be using the most up-to-date index, so whenever I do update the index, I destroy the old searcher, and then recreate it again the next time it&#8217;s needed.</p>
<p>Since IIS is handling the HTTP requests with multiple threads, these searches are happening on multiple threads.   Each search tries to grab my one-and-only lock, the one that keeps the updating threads from conflicting with each other and that keeps the updating threads from conflicting with searches.     Because there is just this one-and-only lock, all the searches on the website have to line up in single-file to get through this bottleneck.   Sounds terrible, doesn&#8217;t it?   But so far, no reports of any problems.   It&#8217;s just a bug tracker, not twitter, and so I can get away with this design, and there&#8217;s no confusion ever about people doing searches with out-of-date indexes.</p>
<div style="margin-left:40px;"><span style="color:#000000;">Lucene.Net.QueryParsers.QueryParser parser = <span style="background-color:#ffff00;">new Lucene.Net.QueryParsers.QueryParser(&#8220;text&#8221;</span>, analyzer );</span><br />
<span style="color:#000000;">Lucene.Net.Search.Query query = null;</span></p>
<p><span style="color:#000000;">try</span><br />
<span style="color:#000000;">{</span><br />
<span style="color:#000000;"> if (string.IsNullOrEmpty(text_user_entered))</span><br />
<span style="color:#000000;"> {</span><br />
<span style="color:#000000;"> throw new Exception(&#8220;You forgot to enter something to search for&#8230;&#8221;);</span><br />
<span style="color:#000000;"> }</span><br />
<span style="color:#000000;"> </span><br />
<span style="color:#000000;"> query = <span style="background-color:#ffff00;">parser.Parse(text_user_entered)</span>;</span><br />
<span style="color:#000000;"> </span><br />
<span style="color:#000000;">}</span><br />
<span style="color:#000000;">catch (Exception e)</span><br />
<span style="color:#000000;">{</span><br />
<span style="color:#000000;"> display_exception(e);</span><br />
<span style="color:#000000;">}</span></p>
<p><span style="color:#000000;">lock (my_lock)</span><br />
<span style="color:#000000;">{</span><br />
<span style="color:#000000;"> </span><br />
<span style="color:#000000;"> Lucene.Net.Search.Hits hits = null;</span><br />
<span style="color:#000000;"> try</span><br />
<span style="color:#000000;"> {</span><br />
<span style="color:#000000;"> if (searcher == null)</span><br />
<span style="color:#000000;"> {</span><br />
<span style="color:#000000;"> searcher = <span style="background-color:#ffff00;">new Lucene.Net.Search.IndexSearcher(&#8220;c:\\folder_where_lucene_index_lives&#8221;);</span></span><br />
<span style="color:#000000;"> }</p>
<p></span><span style="color:#000000;"> <span style="background-color:#ffff00;">hits = searcher.Search(query);</span></span></p>
<p><span style="color:#000000;"> }</span><br />
<span style="color:#000000;"> catch (Exception e)</span><br />
<span style="color:#000000;"> {</span><br />
<span style="color:#000000;"> display_exception(e);</span><br />
<span style="color:#000000;"> }</span><br />
<span style="color:#000000;"> </span><br />
<span style="color:#000000;"> <span style="background-color:#ffff00;">for (int i = 0; i &amp;lt; hits.Length(); i++)</span></span><br />
<span style="color:#000000;"> {</span><br />
<span style="color:#000000;"> <span style="background-color:#ffff00;">Lucene.Net.Documents.Document doc = hits.Doc(i);</span></span><br />
<span style="color:#000000;"> ~~</span><br />
<span style="color:#000000;"> ~~ more processing of the hits and the Lucene docs here ~~</span><br />
<span style="color:#000000;"> ~~</span><br />
<span style="color:#000000;"> }</span><br />
<span style="color:#000000;">}</span></div>
<p><strong>5) Displaying the results</strong></p>
<p>If you didn&#8217;t like my design prior to this point, what with the locking and the bottleneck, then you are going to really hate it now, because it gets weird now.    The search results I get back from Lucene is in the form of a Hits object, a collection of hits that you access by index.   The collection is in the order of the probability score, which you can get using the Hits.Score() method.   You can also get at the Lucene Document related to the hit via the Hits.Doc() method.</p>
<p>Now, back when I was designing my Lucene Document, I had to be thinking ahead regarding how I would display the results.   Would I display the results based purely on what&#8217;s in the document?   If so, then I would have had to add fields to the doc for everything I wanted to eventually be displaying, not just the fields I needed for search.   The more fields I put in the doc, the more I would have to be updating the doc and the index to keep it in sync with my database, and the more I would be duplicating database data in the Lucene index.   So, there was a downside to relying strickly on the Lucene doc for my display.</p>
<p>Also, and for me more importantly, I already have a page in my app that knows how to display a list of bugs based on the result of a SQL query.   I didn&#8217;t want to have to adopt that page to work with a Lucene Hits object.    I wanted to somehow convert the Lucene results into the format expected by that existing page.</p>
<p>So, I decided to try importing the Hits into the database, then letting my existing page fetch the hits out of the database, joining the hits to my bugs table to pick up the fields that I had not bothered to duplicate in the Lucene doc as fields.</p>
<p>The code below shows how I imported the Lucene hits into the database.    In short, I create a big batch of SQL Statements and execute them in one trip to the server.    The batch of SQL Statements creates a temporary table with a unique name plus a bunch of insert statements, one for every Lucene hit I want to import and display.    I import the best 100 hits, which is more than enough.   Lucene can find multiple hits in the same document, but I only want to list a given bug once in the search results, so I have logic for that below, the dict_already_seen_ids.</p>
<p>You will probably want to show your users the text around where the hit is, with the searched-for words highlighted, displayed in their context.   Lucene can prepare that displayable snippet of text for your.   You have to create a bunch of Lucene objects, a Formatter, a SimplerFragmenter, a QueryScorer, a Highlighter, etc, as does my code below.   I specified a snippet length of 400 characters and I specified the highlighting to be done using this HTML:  &lt;span style=&#8217;background:yellow;&#8217;&gt;&lt;/span&gt;.    I feed to the highlighter the original Query and the raw text that I had saved in the doc.   Lucene then gave me the formatted, highlighted snippets, which I inserted into my temporary database table.</p>
<p>You might think that the import of the Lucene hits into the database would perform poorly, but actually, it&#8217;s fast.    Had this not worked, then my plan B would have been to create a more complete Lucene Doc, and then somehow programmatically synthesize an ADO.NET recordset for my page downstream that displays results.</p></div>
<div style="margin-left:40px;"><span style="color:#000000;">Lucene.Net.Highlight.Formatter formatter = new Lucene.Net.Highlight.SimpleHTMLFormatter(</span><br />
<span style="color:#000000;"> <span style="background-color:#ffff00;">&#8220;&lt;span style=&#8217;background:yellow;&#8217;&gt;&#8221;</span>,</span><br />
<span style="color:#000000;"> &#8220;&lt;/span&gt;&#8221;);</span></p>
<p><span style="color:#000000;">Lucene.Net.Highlight.SimpleFragmenter fragmenter = new Lucene.Net.Highlight.SimpleFragmenter(<span style="background-color:#ffff00;">400</span>);</span><br />
<span style="color:#000000;">Lucene.Net.Highlight.QueryScorer scorer = new Lucene.Net.Highlight.QueryScorer(query);</span><br />
<span style="color:#000000;">Lucene.Net.Highlight.Highlighter highlighter = new Lucene.Net.Highlight.Highlighter(formatter, scorer);</span><br />
<span style="color:#000000;">highlighter.SetTextFragmenter(fragmenter); </span></p>
<p><span style="color:#000000;">StringBuilder sb = new StringBuilder();</span><br />
<span style="color:#000000;">string guid = Guid.NewGuid().ToString().Replace(&#8220;-&#8221;, &#8221;");</span><br />
<span style="color:#000000;">Dictionary&amp;lt;string, int&amp;gt; dict_already_seen_ids = new Dictionary&amp;lt;string, int&amp;gt;();</span><br />
<span style="color:#000000;">sb.Append(@&#8221;</span><br />
<span style="color:#000000;"> <span style="background-color:#ffff00;">create table #$GUID</span></span><br />
<span style="color:#000000;"> (</span><br />
<span style="color:#000000;"> temp_bg_id int,</span><br />
<span style="color:#000000;"> temp_score float,</span><br />
<span style="color:#000000;"> temp_text nvarchar(3000)</span><br />
<span style="color:#000000;"> )</span><br />
<span style="color:#000000;">&#8220;);</span></p>
<p><span style="color:#000000;">// insert the search results into a temp table which we will join with what&#8217;s in the database</span><br />
<span style="color:#000000;">for (int i = 0; i &lt; hits.Length(); i++)</span><br />
<span style="color:#000000;">{</span><br />
<span style="color:#000000;"> if (dict_already_seen_ids.Count &lt; 100)</span><br />
<span style="color:#000000;"> {</span><br />
<span style="color:#000000;"> Lucene.Net.Documents.Document doc = hits.Doc(i);</span><br />
<span style="color:#000000;"> string bg_id = doc.Get(&#8220;bg_id&#8221;);</span><br />
<span style="color:#000000;"> if (!dict_already_seen_ids.ContainsKey(bg_id))</span><br />
<span style="color:#000000;"> {</span><br />
<span style="color:#000000;"> dict_already_seen_ids[bg_id] = 1;</span><br />
<span style="color:#000000;"> sb.Append(&#8220;<span style="background-color:#ffff00;">insert into</span> #&#8221;);</span><br />
<span style="color:#000000;"> sb.Append(guid);</span><br />
<span style="color:#000000;"> sb.Append(&#8220; values(&#8220;);</span><br />
<span style="color:#000000;"> sb.Append(bg_id);</span><br />
<span style="color:#000000;"> sb.Append(&#8220;,&#8221;);</span><br />
<span style="color:#000000;"> //sb.Append(Convert.ToString((hits.Score(i))));</span><br />
<span style="color:#000000;"> sb.Append(Convert.ToString((hits.Score(i))).Replace(&#8220;,&#8221;, &#8221;.&#8221;));  // Somebody said this fixes a bug. Localization issue?</span><br />
<span style="color:#000000;"> sb.Append(&#8220;,N&#8217;&#8221;);</span><br />
<span style="color:#000000;"> </span><br />
<span style="color:#000000;"> string raw_text = Server.HtmlEncode(doc.Get(&#8220;raw_text&#8221;));</span><br />
<span style="color:#000000;"></p>
<p><span style="background-color:#ffff00;">Lucene.Net.Analysis.TokenStream stream = analyzer.TokenStream(&#8220;&#8221;, new System.IO.StringReader(raw_text));</span></span><br />
<span style="color:#000000;"> <span style="background-color:#ffff00;">string highlighted_text = highlighter.GetBestFragments(stream, raw_text, 1, &#8221;&#8230;&#8221;).Replace(&#8220;&#8216;&#8221;, &#8221;&#8221;&#8221;);</span></span><br />
<span style="color:#000000;"></p>
<p>if (highlighted_text == &#8221;") // someties the highlighter fails to emit text&#8230;</span><br />
<span style="color:#000000;"> {</span><br />
<span style="color:#000000;"> highlighted_text = raw_text.Replace(&#8220;&#8216;&#8221;,&#8221;&#8221;&#8221;);</span><br />
<span style="color:#000000;"> }</span><br />
<span style="color:#000000;"> if (highlighted_text.Length &gt; 3000)</span><br />
<span style="color:#000000;"> {</span><br />
<span style="color:#000000;"> highlighted_text = highlighted_text.Substring(0,3000);</span><br />
<span style="color:#000000;"> }</span><br />
<span style="color:#000000;"> sb.Append(highlighted_text);</span><br />
<span style="color:#000000;"> sb.Append(&#8220;&#8216;&#8221;);</span><br />
<span style="color:#000000;"> sb.Append(&#8220;)\n&#8221;);</span><br />
<span style="color:#000000;"> }</span><br />
<span style="color:#000000;"> }</span><br />
<span style="color:#000000;"> else</span><br />
<span style="color:#000000;"> {</span><br />
<span style="color:#000000;"> break;</span><br />
<span style="color:#000000;"> }</span><br />
<span style="color:#000000;">}</span></div>
<div style="margin-left:40px;"></div>
<p>We&#8217;re done.  I&#8217;d be very interested in your feedback.   Was my explanation here helpful to you?   Were my design choices stupid?   I&#8217;d like to hear from you.</p>
<p><a href="http://www.ifdefined.com/blog/post/2009/02/Full-Text-Search-in-ASPNET-using-LuceneNET.aspx" target="_blank">http://www.ifdefined.com/blog/post/2009/02/Full-Text-Search-in-ASPNET-using-LuceneNET.aspx</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/khanhpt.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/khanhpt.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/khanhpt.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/khanhpt.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/khanhpt.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/khanhpt.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/khanhpt.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/khanhpt.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/khanhpt.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/khanhpt.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/khanhpt.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/khanhpt.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/khanhpt.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/khanhpt.wordpress.com/53/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=khanhpt.wordpress.com&amp;blog=4686976&amp;post=53&amp;subd=khanhpt&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://khanhpt.wordpress.com/2009/04/02/full-text-search-in-aspnet-using-lucenenet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/72008dc052f08cf83608b61df719edc9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">khanhpt</media:title>
		</media:content>
	</item>
		<item>
		<title>Load a XmlDocument in asp.net</title>
		<link>http://khanhpt.wordpress.com/2009/04/01/load-a-xmldocument-in-aspnet/</link>
		<comments>http://khanhpt.wordpress.com/2009/04/01/load-a-xmldocument-in-aspnet/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 09:03:53 +0000</pubDate>
		<dc:creator>khanhpt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://khanhpt.wordpress.com/?p=50</guid>
		<description><![CDATA[Loading XML Documents Loading of an XML document is accomplished by calling the Load() method, which reads XML data and populates the document tree structure. There are four different versions of the Load() method, each of which uses a different source to read the data. Here are the various forms of the Load() method: ❑ [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=khanhpt.wordpress.com&amp;blog=4686976&amp;post=50&amp;subd=khanhpt&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Loading XML Documents<br />
Loading of an XML document is accomplished by calling the Load() method, which reads XML data<br />
and populates the document tree structure. There are four different versions of the Load() method, each<br />
of which uses a different source to read the data. Here are the various forms of the Load() method:<br />
❑ Load(Stream): Loads the document from a Stream data source<br />
❑ Load(string): Loads the document using the given file name string<br />
❑ Load(TextReader): Loads the document using a TextReader as the data source<br />
❑ Load(XmlReader): Loads the document using the given XmlReader as the data source<br />
In addition to taking a Stream, TextReader, and XmlReader objects, the Load() method also takes in a<br />
file name as a string argument. Using this method, you can load an XML document from the specified<br />
URL. Apart from the overloaded Load() methods, there is also a method named LoadXml() that makes<br />
it possible to load the XML document from a string of data as its argument.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/khanhpt.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/khanhpt.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/khanhpt.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/khanhpt.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/khanhpt.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/khanhpt.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/khanhpt.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/khanhpt.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/khanhpt.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/khanhpt.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/khanhpt.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/khanhpt.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/khanhpt.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/khanhpt.wordpress.com/50/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=khanhpt.wordpress.com&amp;blog=4686976&amp;post=50&amp;subd=khanhpt&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://khanhpt.wordpress.com/2009/04/01/load-a-xmldocument-in-aspnet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/72008dc052f08cf83608b61df719edc9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">khanhpt</media:title>
		</media:content>
	</item>
		<item>
		<title>Cơ bản về XML và DOM</title>
		<link>http://khanhpt.wordpress.com/2009/04/01/c%c6%a1-b%e1%ba%a3n-v%e1%bb%81-xml-va-dom/</link>
		<comments>http://khanhpt.wordpress.com/2009/04/01/c%c6%a1-b%e1%ba%a3n-v%e1%bb%81-xml-va-dom/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 08:52:46 +0000</pubDate>
		<dc:creator>khanhpt</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://khanhpt.wordpress.com/?p=48</guid>
		<description><![CDATA[Exploring DOM Processing The DOM is a specification for an API that lets programmers manipulates XML held in memory. The DOM specification is language-independent, and bindings are available for many programming lan- guages, including C++. XmlDocument is based upon the DOM, with Microsoft extensions. Because it works with XML in memory, it has several advantages [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=khanhpt.wordpress.com&amp;blog=4686976&amp;post=48&amp;subd=khanhpt&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:left;"><a href="http://toptiptut.com"><strong>Exploring DOM Processing</strong></a></p>
<p style="text-align:left;">The DOM is a specification for an API that lets programmers manipulates XML held in memory. The<br />
DOM specification is language-independent, and bindings are available for many programming lan-<br />
guages, including C++. XmlDocument is based upon the DOM, with Microsoft extensions. Because it<br />
works with XML in memory, it has several advantages and disadvantages over the XmlReader forward-<br />
only approach.<br />
One advantage is that, in reading the entire document and building a tree in memory, you have access to all<br />
the elements and can wander through the document at will. You can also edit the document, changing,<br />
adding, or deleting nodes, and write the changed document back to disk again. It is even possible to create<br />
an entire XML document from scratch in memory and write it out—serialize it—and this is a useful alter-<br />
native to using XmlWriter.<br />
The main disadvantage is that the whole of an XML document is held in memory at once, so the amount<br />
of memory needed by your program is going to be proportional to the size of the XML document you’re<br />
working with. This means that if you’re working with a very large XML document—or have limited<br />
memory—you might not be able to use XmlDocument. Another disadvantage is that the API for the<br />
XmlDocument is specified through the DOM model, limiting the options for implementing performance<br />
improvements behind the XmlDocument class.</p>
<p style="text-align:left;"><strong><br />
</strong></p>
<p style="text-align:left;"><strong>XML Document Loaded in a DOM Tree</strong><br />
Working with an XmlDocument class is fundamentally different from working with the XmlReader and<br />
XmlWriter classes, although they share some similarities, such as the concept of nodes. When you use<br />
DOM processing, the entire XML document is loaded into a tree structure that matches the hierarchical<br />
structure of the document. The DOM provides a set of functions that examine the tree structure and<br />
manipulate the document’s content. To see an example of this, consider the XML document shown in<br />
Listing 6-1 that contains a simple book store along with information about the various books that are<br />
part of the bookstore.<br />
Listing 6-1: XML Document That Represents the Bookstore</p>
<blockquote>
<p style="text-align:left;">
&lt;?xml version=’1.0’?&gt;<br />
&lt;!&#8211; This file represents a fragment of a book store inventory database &#8211;&gt;<br />
&lt;bookstore&gt;<br />
&lt;book genre=”autobiography”&gt;<br />
&lt;title&gt;The Autobiography of Benjamin Franklin&lt;/title&gt;<br />
&lt;author&gt;<br />
&lt;first-name&gt;Benjamin&lt;/first-name&gt;<br />
&lt;last-name&gt;Franklin&lt;/last-name&gt;</p>
<p style="text-align:left;">&lt;/author&gt;</p>
<p style="text-align:left;">&lt;price&gt;8.99&lt;/price&gt;<br />
&lt;/book&gt;<br />
&lt;book genre=”novel”&gt;<br />
&lt;title&gt;The Confidence Man&lt;/title&gt;<br />
&lt;author&gt;<br />
&lt;first-name&gt;Herman&lt;/first-name&gt;<br />
&lt;last-name&gt;Melville&lt;/last-name&gt;<br />
&lt;/author&gt;<br />
&lt;price&gt;11.99&lt;/price&gt;<br />
&lt;/book&gt;<br />
&lt;book genre=”philosophy”&gt;<br />
&lt;title&gt;The Gorgias&lt;/title&gt;<br />
&lt;author&gt;<br />
&lt;name&gt;Plato&lt;/name&gt;<br />
&lt;/author&gt;<br />
&lt;price&gt;9.99&lt;/price&gt;<br />
&lt;/book&gt;<br />
&lt;/bookstore&gt;</p></blockquote>
<p style="text-align:left;">
<p style="text-align:left;">Note that the books.xml file shown in Listing 6-1 is used throughout all the examples presented in this<br />
chapter. An XML document has a tree structure under the DOM, and each object in the tree is a node. T<br />
document, modeled using the DOM, would be represented as the tree structure shown in Figure 6-1.</p>
<p style="text-align:left;">
<div class="wp-caption alignleft" style="width: 856px"><img title="domtree" src="http://i38.photobucket.com/albums/e117/khanhpt/domtree.jpg" alt="domtree" width="846" height="582" /><p class="wp-caption-text">domtree</p></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/khanhpt.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/khanhpt.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/khanhpt.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/khanhpt.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/khanhpt.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/khanhpt.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/khanhpt.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/khanhpt.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/khanhpt.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/khanhpt.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/khanhpt.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/khanhpt.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/khanhpt.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/khanhpt.wordpress.com/48/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=khanhpt.wordpress.com&amp;blog=4686976&amp;post=48&amp;subd=khanhpt&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://khanhpt.wordpress.com/2009/04/01/c%c6%a1-b%e1%ba%a3n-v%e1%bb%81-xml-va-dom/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/72008dc052f08cf83608b61df719edc9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">khanhpt</media:title>
		</media:content>

		<media:content url="http://i38.photobucket.com/albums/e117/khanhpt/domtree.jpg" medium="image">
			<media:title type="html">domtree</media:title>
		</media:content>
	</item>
		<item>
		<title>Một số hàm thường dùng trong ASP Membership Provider</title>
		<link>http://khanhpt.wordpress.com/2009/01/14/m%e1%bb%99t-s%e1%bb%91-ham-th%c6%b0%e1%bb%9dng-dung-trong-asp-membership-provider/</link>
		<comments>http://khanhpt.wordpress.com/2009/01/14/m%e1%bb%99t-s%e1%bb%91-ham-th%c6%b0%e1%bb%9dng-dung-trong-asp-membership-provider/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 07:55:11 +0000</pubDate>
		<dc:creator>khanhpt</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Asp.Net with C#]]></category>
		<category><![CDATA[asp]]></category>
		<category><![CDATA[membership]]></category>

		<guid isPermaLink="false">http://khanhpt.wordpress.com/?p=41</guid>
		<description><![CDATA[Tóm lượt từ các tutorial tại www.asp.net Create User: link gốc : http://www.asp.net/learn/security/tutorial-05-cs.aspx MembershipCreateStatus createStatus; MembershipUser newUser = Membership.CreateUser(Username.Text, Password.Text, Email.Text, passwordQuestion, SecurityAnswer.Text, true, out createStatus); kiểm tra quá trình create user bằng creatStatus. Kiểm tra quyền của user: link : http://www.asp.net/learn/security/tutorial-08-cs.aspx Get a reference to the currently logged on user MembershipUser currentUser = Membership.GetUser(); [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=khanhpt.wordpress.com&amp;blog=4686976&amp;post=41&amp;subd=khanhpt&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Tóm lượt từ các tutorial tại www.asp.net</p>
<p><strong>Create User:</strong></p>
<p>link gốc : http://www.asp.net/learn/security/tutorial-05-cs.aspx</p>
<p><code class="code_block">MembershipCreateStatus createStatus; </code></p>
<p><code class="code_block">MembershipUser newUser = Membership.CreateUser(Username.Text, Password.Text, Email.Text, passwordQuestion, SecurityAnswer.Text, true, out createStatus); </code></p>
<p>kiểm tra quá trình create user bằng creatStatus.</p>
<p><strong>Kiểm tra quyền của user</strong>:</p>
<p>link : http://www.asp.net/learn/security/tutorial-08-cs.aspx</p>
<p><code class="code_block"> <em>Get a reference to the currently logged on user </em> </code></p>
<blockquote><p><code class="code_block">MembershipUser currentUser = Membership.GetUser(); </code></p></blockquote>
<p><em><code class="code_block"> Determine the currently logged on user's UserId value </code></em></p>
<blockquote><p><code class="code_block">Guid currentUserId = (Guid)currentUser.ProviderUserKey; </code></p></blockquote>
<p><strong>Kiểm tra Role của user hiện tại:</strong></p>
<p><code class="code_block"> bool allow = (</code><code class="code_block">currentUser</code><code class="code_block">.IsInRole("Role cần kiểm tra"))<br />
</code></p>
<p>Tạm thời copy 2 em thường dùng nhưng cũng thường quên nhất <img src='http://s0.wp.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/khanhpt.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/khanhpt.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/khanhpt.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/khanhpt.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/khanhpt.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/khanhpt.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/khanhpt.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/khanhpt.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/khanhpt.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/khanhpt.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/khanhpt.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/khanhpt.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/khanhpt.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/khanhpt.wordpress.com/41/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=khanhpt.wordpress.com&amp;blog=4686976&amp;post=41&amp;subd=khanhpt&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://khanhpt.wordpress.com/2009/01/14/m%e1%bb%99t-s%e1%bb%91-ham-th%c6%b0%e1%bb%9dng-dung-trong-asp-membership-provider/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/72008dc052f08cf83608b61df719edc9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">khanhpt</media:title>
		</media:content>
	</item>
		<item>
		<title>RemoveHTML</title>
		<link>http://khanhpt.wordpress.com/2009/01/04/removehtml/</link>
		<comments>http://khanhpt.wordpress.com/2009/01/04/removehtml/#comments</comments>
		<pubDate>Sun, 04 Jan 2009 15:33:31 +0000</pubDate>
		<dc:creator>khanhpt</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Asp.Net with C#]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[remove]]></category>

		<guid isPermaLink="false">http://khanhpt.wordpress.com/?p=35</guid>
		<description><![CDATA[using System.Text.RegularExpressions; ... public static string RemoveHTML(string in_HTML) { return Regex.Replace(in_HTML, "&#60;(.&#124;\n)*?&#62;", ""); }<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=khanhpt.wordpress.com&amp;blog=4686976&amp;post=35&amp;subd=khanhpt&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<pre>using System.Text.RegularExpressions;

  ...

  public static string RemoveHTML(string in_HTML)
    {
      return Regex.Replace(in_HTML, "&lt;(.|\n)*?&gt;", "");
    }</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/khanhpt.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/khanhpt.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/khanhpt.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/khanhpt.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/khanhpt.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/khanhpt.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/khanhpt.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/khanhpt.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/khanhpt.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/khanhpt.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/khanhpt.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/khanhpt.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/khanhpt.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/khanhpt.wordpress.com/35/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=khanhpt.wordpress.com&amp;blog=4686976&amp;post=35&amp;subd=khanhpt&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://khanhpt.wordpress.com/2009/01/04/removehtml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/72008dc052f08cf83608b61df719edc9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">khanhpt</media:title>
		</media:content>
	</item>
		<item>
		<title>HOW TO: Rewrite URL / Implement URL Rewriting in ASP.NET</title>
		<link>http://khanhpt.wordpress.com/2008/12/11/how-to-rewrite-url-implement-url-rewriting-in-aspnet/</link>
		<comments>http://khanhpt.wordpress.com/2008/12/11/how-to-rewrite-url-implement-url-rewriting-in-aspnet/#comments</comments>
		<pubDate>Thu, 11 Dec 2008 04:21:05 +0000</pubDate>
		<dc:creator>khanhpt</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Asp.Net with C#]]></category>

		<guid isPermaLink="false">http://khanhpt.wordpress.com/?p=33</guid>
		<description><![CDATA[Solution Reference articles: Tip/Trick: Url Rewriting with ASP.NET by Scott Guthrie &#8211; Examines four approaches: 1) Use Request.PathInfo Parameters Instead of QueryStrings; 2) Using an HttpModule to Perform URL Rewriting; 3) Using an HttpModule to Perform Extension-Less URL Rewriting with IIS7; 4) ISAPIRewrite to enable Extension-less URL Rewriting for IIS5 and IIS6; also discusses how [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=khanhpt.wordpress.com&amp;blog=4686976&amp;post=33&amp;subd=khanhpt&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h3>Solution</h3>
<p><strong>Reference articles:</strong></p>
<ul>
<li><a href="http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx" target="_blank">Tip/Trick: Url Rewriting with ASP.NET</a> by Scott Guthrie &#8211; Examines four approaches: 1) Use Request.PathInfo Parameters Instead of QueryStrings; 2) Using an HttpModule to Perform URL Rewriting; 3) Using an HttpModule to Perform Extension-Less URL Rewriting with IIS7; 4) ISAPIRewrite to enable Extension-less URL Rewriting for IIS5 and IIS6; also discusses how to handle ASP.NET postbacks correctly with URL rewriting.</li>
</ul>
<ul>
<li><a href="http://www.csharpfriends.com/Articles/getTip.aspx?articleID=215" target="_blank">URL                 Rewriting</a> by Salman (CSharpFriends) &#8211; simple implementation of URL rewriting logic                 within the Application_BeginRequest() method of the Global.asax file.</li>
</ul>
<ul>
<li><a href="http://www.theukwebdesigncompany.com/articles/search-engine-friendly-urls-asp.php" target="_blank">Search                 Engine Friendly URLs using ASP.NET (C#.NET)</a> by Maziar Aflatoun (The UK Web Design                 Company) &#8211; similar to the above, but regular expression is used to match URLs.</li>
</ul>
<ul>
<li><a href="http://www.15seconds.com/issue/030522.htm" target="_blank">Rewrite.NET &#8211;                 A URL Rewriting Engine for .NET</a> by Robert Chartier (15Seconds.com). Solution steps:
<ul>
<li> Create the HttpModule to process the web request and rewrite the URL</li>
<li> Add the handler in Web.config</li>
<li> Create a configuration section in Web.config to define URL mapping rules</li>
<li> Add extensibility by creating a rules engine interface</li>
<li> Write class or classes to implement rules engine interface</li>
<li> Add code to the HttpModule to dynamically load the desired rules from Web.config</li>
</ul>
</li>
</ul>
<ul>
<li><a href="http://msdn.microsoft.com/asp.net/using/building/web/default.aspx?pull=/library/en-us/dnaspp/html/urlrewriting.asp" target="_blank">URL                 Rewriting in ASP.NET</a> by Scott Mitchell (MSDN) &#8211; examines how to implement URL                 rewriting in a HTTP module; also explains how to handle postbacks.</li>
</ul>
<ul>
<li><a href="http://www.codeproject.com/aspnet/URLRewriter.asp" target="_blank">URL Rewriting                 with ASP.NET</a> by Richard Birkby (CodeProject) &#8211; shows how legacy ASP sites can                 be upgraded to ASP.NET, while maintaining links from search engines. Solution steps:
<ul>
<li> Create the configuration section in Web.config for defining URL mapping rules</li>
<li> Write the configuration section handler class, incorporating the URL rewriting logic</li>
<li> Create a call to handler in Global.asax in Application_BeginRequest() method</li>
<li> Compile the code and install the rewriter assembly in the Global Assembly Cache (GAC)</li>
<li> Configure IIS to map non- .aspx files to the ASP.NET ISAPI extension</li>
</ul>
</li>
</ul>
<h3>Related Resources</h3>
<ul>
<li><a href="http://west-wind.com/weblog/posts/269.aspx" target="_blank">Making Sense                 of ASP.NET Paths</a> by Rick Strahl</li>
<li><a href="http://weblogs.asp.net/jezell/archive/2004/03/15/90045.aspx" target="_blank">Fixing                 Microsoft&#8217;s Bugs: URL Rewriting</a> by Jesse Ezell</li>
<li><a href="http://www.aspnetworld.com/store/itemdetails/B0000632ZU.aspx">Regular Expressions with .NET</a> by                 Dan Appleman and Daniel Appleman</li>
<li><a href="http://www.urlrewriting.net/" target="_blank">UrlRewritingNet.UrlRewrite</a> &#8211; an Open Source Component which allows you certainly to rewrite URLs with ASP.NET  2.0.</li>
<li><a href="http://urlrewriter.net/" target="_blank">Open Source URL Rewriter for .NET / IIS / ASP.NET</a></li>
</ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/khanhpt.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/khanhpt.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/khanhpt.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/khanhpt.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/khanhpt.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/khanhpt.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/khanhpt.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/khanhpt.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/khanhpt.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/khanhpt.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/khanhpt.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/khanhpt.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/khanhpt.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/khanhpt.wordpress.com/33/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=khanhpt.wordpress.com&amp;blog=4686976&amp;post=33&amp;subd=khanhpt&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://khanhpt.wordpress.com/2008/12/11/how-to-rewrite-url-implement-url-rewriting-in-aspnet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/72008dc052f08cf83608b61df719edc9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">khanhpt</media:title>
		</media:content>
	</item>
	</channel>
</rss>
