<?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/"
	>

<channel>
	<title>jesal gadhia &#187; xml</title>
	<atom:link href="http://jesal.us/tag/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://jesal.us</link>
	<description></description>
	<lastBuildDate>Thu, 14 Jul 2011 18:51:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>How To Create A Random Fact Generator Using XML</title>
		<link>http://jesal.us/2008/04/how-to-create-a-random-fact-generator-using-xml/</link>
		<comments>http://jesal.us/2008/04/how-to-create-a-random-fact-generator-using-xml/#comments</comments>
		<pubDate>Thu, 24 Apr 2008 00:03:19 +0000</pubDate>
		<dc:creator>J</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://jesal.us/blog/?p=90</guid>
		<description><![CDATA[This is a simple little random fact generator which will show a new fact every time the page loads. After the initial load it will store the XML in the cache until the file is changed again. XML: (Facts.xml) &#60;?xml version="1.0" encoding="utf-8" ?&#62; &#60;facts&#62; &#60;fact&#62; The numbers '172' can be found on the back of [...]]]></description>
			<content:encoded><![CDATA[<p>This is a simple little random fact generator which will show a new fact every time the page loads. After the initial load it will store the XML in the cache until the file is changed again.</p>
<p><b>XML: (Facts.xml)</b></p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">
<span class="kwrd">&lt;?</span><span class="html">xml</span> <span class="attr">version</span><span class="kwrd">="1.0"</span> <span class="attr">encoding</span><span class="kwrd">="utf-8"</span> ?<span class="kwrd">&gt;</span>
<span class="kwrd">&lt;</span><span class="html">facts</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">fact</span><span class="kwrd">&gt;</span>
    The numbers '172' can be found on the back of the U.S. $5 dollar
    bill in the bushes at the base of the Lincoln Memorial.
  <span class="kwrd">&lt;/</span><span class="html">fact</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">fact</span><span class="kwrd">&gt;</span>
    President Kennedy was the fastest random speaker in the world
    with upwards of 350 words per minute.
  <span class="kwrd">&lt;/</span><span class="html">fact</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">fact</span><span class="kwrd">&gt;</span>
    In the average lifetime, a person will walk the equivalent of 5
    times around the equator.
  <span class="kwrd">&lt;/</span><span class="html">fact</span><span class="kwrd">&gt;</span>
    .
    .
    .
<span class="kwrd">&lt;/</span><span class="html">facts</span><span class="kwrd">&gt;</span></pre>
<p><b>Code: (RandomFact.ascx.cs)</b></p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre>
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">using</span> System;
<p style="margin: 0px;"><span style="color: blue;">using</span> System.Data;
<p style="margin: 0px;"><span style="color: blue;">using</span> System.Configuration;
<p style="margin: 0px;"><span style="color: blue;">using</span> System.Collections;
<p style="margin: 0px;"><span style="color: blue;">using</span> System.Web;
<p style="margin: 0px;"><span style="color: blue;">using</span> System.Web.Caching;
<p style="margin: 0px;"><span style="color: blue;">using</span> System.Web.Security;
<p style="margin: 0px;"><span style="color: blue;">using</span> System.Web.UI;
<p style="margin: 0px;"><span style="color: blue;">using</span> System.Web.UI.WebControls;
<p style="margin: 0px;"><span style="color: blue;">using</span> System.Web.UI.WebControls.WebParts;
<p style="margin: 0px;"><span style="color: blue;">using</span> System.Web.UI.HtmlControls;
<p style="margin: 0px;"><span style="color: blue;">using</span> System.Xml;
<p style="margin: 0px;"><span style="color: blue;">using</span> System.IO;
<p style="margin: 0px;"><span style="color: blue;">using</span> System.ComponentModel;
<p style="margin: 0px;"><span style="color: blue;">using</span> System.Drawing.Design;
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">partial</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">_controls_RandomFact</span> : System.Web.UI.<span style="color: #2b91af;">UserControl</span>
<p style="margin: 0px;">{
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">string</span> _xmlDataSource;
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; [<span style="color: #2b91af;">UrlProperty</span>()]
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> XMLDataSource
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> { <span style="color: blue;">return</span> _xmlDataSource; }
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">set</span> { _xmlDataSource = <span style="color: blue;">value</span>; }
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; }
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">protected</span> <span style="color: blue;">void</span> Page_Load(<span style="color: blue;">object</span> sender, <span style="color: #2b91af;">EventArgs</span> e)
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; litFact.Text = getRandomFact();
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; }
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">string</span> getRandomFact()
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Random</span> rndIndex = <span style="color: blue;">new</span> <span style="color: #2b91af;">Random</span>();
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">XmlDocument</span> xmlDocFacts = <span style="color: blue;">new</span> <span style="color: #2b91af;">XmlDocument</span>();
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">string</span> strFact = <span style="color: blue;">string</span>.Empty;
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">try</span>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (Cache[<span style="color: #a31515;">"xmlDocFacts"</span>] != <span style="color: blue;">null</span>)
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; xmlDocFacts = (<span style="color: #2b91af;">XmlDocument</span>)Cache[<span style="color: #a31515;">"xmlDocFacts"</span>];
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">else</span>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; xmlDocFacts.Load(Server.MapPath(XMLDataSource));
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Cache.Insert(<span style="color: #a31515;">"xmlDocFacts"</span>, xmlDocFacts, <span style="color: blue;">new</span> <span style="color: #2b91af;">CacheDependency</span>(Server.MapPath(XMLDataSource)));
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">XmlNodeList</span> xmlNodesMessage = xmlDocFacts.SelectNodes(<span style="color: #a31515;">"//fact"</span>);
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">int</span> rnd = rndIndex.Next(0, xmlNodesMessage.Count);
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; strFact = Server.HtmlEncode(xmlNodesMessage[rnd].InnerText);
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">catch</span> (<span style="color: #2b91af;">Exception</span> ex)
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; strFact = <span style="color: blue;">string</span>.Format(<span style="color: #a31515;">"&lt;b&gt;Error:&lt;/b&gt; {0}"</span>, ex.Message);
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> strFact;
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; }
<p style="margin: 0px;">}
</div>
</pre>
<p><b>Usage: (Default.aspx)</b><br />
<br/></p>
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">&lt;</span><span style="color: #a31515;">uc1</span><span style="color: blue;">:</span><span style="color: #a31515;">RandomFact</span> <span style="color: red;">ID</span><span style="color: blue;">=&#8221;RandomFact1&#8243;</span> <span style="color: red;">runat</span><span style="color: blue;">=&#8221;server&#8221;</span> <span style="color: red;">XMLDataSource</span><span style="color: blue;">=&#8221;App_Data/Facts.xml&#8221;</span> <span style="color: blue;">/&gt;</span></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://jesal.us/2008/04/how-to-create-a-random-fact-generator-using-xml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

