<?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>Antferr &#187; Tutorial</title>
	<atom:link href="http://www.antferr.com/category/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.antferr.com</link>
	<description>Information and communication technology me.think.toString(&#34;blog&#34;);</description>
	<lastBuildDate>Mon, 17 Oct 2011 08:44:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>RESTful API JSON Poker Texas Hold&#8217;em WCF C#</title>
		<link>http://www.antferr.com/c/restful-api-texas-holdem-wcf-c/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=restful-api-texas-holdem-wcf-c</link>
		<comments>http://www.antferr.com/c/restful-api-texas-holdem-wcf-c/#comments</comments>
		<pubDate>Thu, 13 Oct 2011 13:37:10 +0000</pubDate>
		<dc:creator>antferr</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[RESTful]]></category>

		<guid isPermaLink="false">http://www.antferr.com/?p=605</guid>
		<description><![CDATA[
			
				
			
		
Negli ultimi anni il Poker, nella sua accezione del Texas Hold&#8217;em, è diventato il gioco più amato dagli italiani, scavalcato solo dalle scommesse, legalizzate da molto più tempo. Al momento, secondo l’istituto di ricerca Eurispes, quella del gioco d’azzardo è la terza industria del  [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.antferr.com%2Fc%2Frestful-api-texas-holdem-wcf-c%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.antferr.com%2Fc%2Frestful-api-texas-holdem-wcf-c%2F&amp;source=antferr&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Negli ultimi anni il Poker, nella sua accezione del Texas Hold&#8217;em, è diventato il gioco più amato dagli italiani, scavalcato solo dalle scommesse, legalizzate da molto più tempo. Al momento, secondo l’istituto di ricerca Eurispes, quella del gioco d’azzardo è la terza industria del paese per volume d’affari. Il fenomeno è supportato da canali televisivi a tema e dal fenomeno del &#8220;divismo&#8221; dei giocatori più conosciuti che espongono sui loro capi di abbigliamento sponsor del <a href="http://www.partypoker.fr/" target="_blank">poker on line.</a><br />

<!-- Begin Google Adsense code -->
<script type="text/javascript"><!--
google_ad_client = "pub-6474961034063636";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
google_ad_channel = "";
//-->
</script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
<br />
Dal punto di vista più strettamente informatico sono interessanti alcuni progetti ospitati su code google con licenza GNU GPL che mettono a disposizione un framework e un logic engine per il il Texas Hold&#8217;em <a href="http://code.google.com/p/openholdembot" target="_blank" rel="nofollow">openholdembot</a>,<a href="http://code.google.com/p/cspoker">cspoker</a>.<br/><br />
Nell&#8217;ipotesi di sviluppare un servizio RESTFull con Microsoft WCF che faccia da dealer e che possa servire come base per sviluppare un software sul poker ipotizziamo come possibile output un flusso JSON.<br />
Creiamo una WCF Service Application chiamata PokerWcfService e cancelliamo il servizio creato di defaul per crearne uno nostro chiamato <em>PokerService.svc</em>. All&#8217;interfaccia <em>IPokerService</em> aggiungiamo un semplice metodo <em>getCards</em> che restituisce che prende in input il numero di giocatori</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><span style="color: #99cc00"></span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p605code4'); return false;">Mostra codice</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6054"><td class="code" id="p605code4"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>OperationContract<span style="color: #008000;">&#93;</span>
<span style="color: #008000;">&#91;</span>WebInvoke<span style="color: #008000;">&#40;</span>Method <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;GET&quot;</span>,
    ResponseFormat <span style="color: #008000;">=</span> WebMessageFormat<span style="color: #008000;">.</span><span style="color: #0000FF;">Json</span>,
    BodyStyle <span style="color: #008000;">=</span> WebMessageBodyStyle<span style="color: #008000;">.</span><span style="color: #0000FF;">Bare</span>,
    UriTemplate <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;getCards/{playersNumber}&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
ResultGetCards getCards<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> playersNumber<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>Per implementare questo metono ho creato una classe ResultGetCards la quale al suo interno contiente una lista di oggetti di tipo PlayerHand e un oggetto ShowDown.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><span style="color: #99cc00"></span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p605code5'); return false;">Mostra codice</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6055"><td class="code" id="p605code5"><pre class="csharp" style="font-family:monospace;">    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Player hand, two cards</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> PlayerHand
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> _firstCard<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> firstCard
        <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _firstCard<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
            set <span style="color: #008000;">&#123;</span> _firstCard <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> _secondCard<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> secondCard
        <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _secondCard<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
            set <span style="color: #008000;">&#123;</span> _secondCard <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// ShowDown Flop</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ShowDown
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> _firstCardFlop<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> firstCardFlop
        <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _firstCardFlop<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
            set <span style="color: #008000;">&#123;</span> _firstCardFlop <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> _secondCardFlop<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> secondCardFlop
        <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _secondCardFlop<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
            set <span style="color: #008000;">&#123;</span> _secondCardFlop <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> _thirdCardFlop<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> thirdCardFlop
        <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _thirdCardFlop<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
            set <span style="color: #008000;">&#123;</span> _thirdCardFlop <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> _turn<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> turn
        <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _turn<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
            set <span style="color: #008000;">&#123;</span> _turn <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> _river<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> river
        <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _river<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
            set <span style="color: #008000;">&#123;</span> _river <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>
<!-- Begin Google Adsense code -->
<script type="text/javascript"><!--
google_ad_client = "pub-6474961034063636";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
google_ad_channel = "";
//-->
</script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
<br />
La classe ResultGetCards risulterebbe quindi strutturata in questo modo:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><span style="color: #99cc00"></span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p605code6'); return false;">Mostra codice</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6056"><td class="code" id="p605code6"><pre class="csharp" style="font-family:monospace;">    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// getCards Output </span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ResultGetCards
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> ShowDown _showDown<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> ShowDown showDown
        <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _showDown<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
            set <span style="color: #008000;">&#123;</span> _showDown <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">private</span> List<span style="color: #008000;">&lt;</span>PlayerHand<span style="color: #008000;">&gt;</span> _playerHands<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> List<span style="color: #008000;">&lt;</span>PlayerHand<span style="color: #008000;">&gt;</span> playerHands
        <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _playerHands<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
            set <span style="color: #008000;">&#123;</span> _playerHands <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Questa è la struttura dei files<br />
<a href="http://www.antferr.com/wp-content/uploads/2011/10/pokerWcfService.jpg"><img src="http://www.antferr.com/wp-content/uploads/2011/10/pokerWcfService.jpg" alt="" title="pokerWcfService" width="338" height="268" class="alignnone size-full wp-image-680" /></a><br />
Una volta creato il servizio, definito il model e strutturata la soluzione non resta che implementare il metodo getCards.<br />
<a href="http://www.antferr.com/wp-content/uploads/2011/10/pokerWcfService2.jpg"><img src="http://www.antferr.com/wp-content/uploads/2011/10/pokerWcfService2-300x296.jpg" alt="" title="getCards" width="300" height="296" class="alignnone size-medium wp-image-689" /></a><br />
La tecnica utilizzata è quella di recuperare dall&#8217;array di stringhe card i valori per popolare il result composto dallo showdown e dalle playerCards senza ripetere mai gli stessi valori e in maniere casuale. Questo è possibile recuperando il numero necessario di valori, in base al numero di giocatori in input, dall&#8217;array che viene ogni volta ordinato in maniera casuale.<br />
L&#8217;output finale sarà del tipo:<br/><br />
<code>{"playerHands":[{"firstCard":"6H","secondCard":"QC"},{"firstCard":"1H","secondCard":"4S"},{"firstCard":"3H","secondCard":"8H"},{"firstCard":"6D","secondCard":"JC"},{"firstCard":"QH","secondCard":"3D"},{"firstCard":"2D","secondCard":"9C"},{"firstCard":"KD","secondCard":"10D"},{"firstCard":"6S","secondCard":"8D"}],"showDown":{"firstCardFlop":"5C","river":"9D","secondCardFlop":"JD","thirdCardFlop":"1S","turn":"1D"}}<br />
</code></p>
<p class="buymebeer"><form action="https://www.paypal.com/cgi-bin/webscr" target="paypal" method="post"><input type="hidden" name="cmd" value="_xclick" /><input type="hidden" name="business" value="antferr@gmail.com" /><input type="hidden" name="return" value="" /><input type="hidden" name="item_name" value="Buy Me a Beer for RESTful API JSON Poker Texas Hold'em WCF C#" /><input type="hidden" name="amount" value="" /><input type="image" src="http://www.antferr.com/wp-content/plugins/buy-me-beer/icon_cafe.gif" align="left" alt="Mi offri un caffè?" title="Mi offri un caffè?" hspace="3" /></form><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&amp;business=antferr@gmail.com&amp;amount=&amp;return=&amp;item_name=Buy+Me+a+Beer+for+RESTful+API+JSON+Poker+Texas+Hold'em+WCF+C#" target="paypal">Se i miei post ti sono stati di aiuto o ti sono sembrati intressanti puoi offrirmi un caffè :)</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.antferr.com/c/restful-api-texas-holdem-wcf-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linq to Xml – gestione news (part 2)</title>
		<link>http://www.antferr.com/aspnet/linq-to-xml-gestione-news-part-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=linq-to-xml-gestione-news-part-2</link>
		<comments>http://www.antferr.com/aspnet/linq-to-xml-gestione-news-part-2/#comments</comments>
		<pubDate>Thu, 08 Jul 2010 08:12:49 +0000</pubDate>
		<dc:creator>antferr</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.antferr.com/?p=531</guid>
		<description><![CDATA[
			
				
			
		
Continuando quanto cominciato con la prima parte di questo tutorial implemento le altre due funzionalità fondamentali, ovvero la cancellazione e l&#8217;aggiornamento di una notizia.







Cancellazione
Come per la creazione creiamo un XDocument con li file delle news, poi individuiamo  [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.antferr.com%2Faspnet%2Flinq-to-xml-gestione-news-part-2%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.antferr.com%2Faspnet%2Flinq-to-xml-gestione-news-part-2%2F&amp;source=antferr&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Continuando quanto cominciato con la <a href="http://www.antferr.com/c/linq-to-xml-gestione-news-part-1/">prima parte</a> di questo tutorial implemento le altre due funzionalità fondamentali, ovvero la cancellazione e l&#8217;aggiornamento di una notizia.<br />

<!-- Begin Google Adsense code -->
<script type="text/javascript"><!--
google_ad_client = "pub-6474961034063636";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
google_ad_channel = "";
//-->
</script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</p>
<h4>Cancellazione</h4>
<p>Come per la creazione creiamo un XDocument con li file delle news, poi individuiamo la notizia con l&#8217;attributo id corrispondete a quella che vogliamo eliminare e facciamo una Remove(). Non ci resta che salvare il file.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><span style="color: #99cc00"></span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p531code9'); return false;">Mostra codice</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p5319"><td class="code" id="p531code9"><pre class="csharp" style="font-family:monospace;"> XDocument newsXML <span style="color: #008000;">=</span> XDocument<span style="color: #008000;">.</span><span style="color: #0000FF;">Load</span><span style="color: #008000;">&#40;</span>Server<span style="color: #008000;">.</span><span style="color: #0000FF;">MapPath</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;App_Data<span style="color: #008080; font-weight: bold;">\\</span>News.xml&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
 newsXML<span style="color: #008000;">.</span><span style="color: #0000FF;">XPathSelectElement</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;News/Notizia[@Id = '&quot;</span> <span style="color: #008000;">+</span> idNews <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;']&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Remove</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
 newsXML<span style="color: #008000;">.</span><span style="color: #0000FF;">Save</span><span style="color: #008000;">&#40;</span>Server<span style="color: #008000;">.</span><span style="color: #0000FF;">MapPath</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;App_Data<span style="color: #008080; font-weight: bold;">\\</span>News.xml&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<h4>Modifica</h4>
<p>Per la modifica utilizziamo la stessa tecnica usata per la cancellazione con l&#8217;unica differenza che non invochiamo la Remove() ma ma aggiorniamo i campi della notizia che intendiamo modificare</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><span style="color: #99cc00"></span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p531code10'); return false;">Mostra codice</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p53110"><td class="code" id="p531code10"><pre class="csharp" style="font-family:monospace;">XDocument newsXML <span style="color: #008000;">=</span> XDocument<span style="color: #008000;">.</span><span style="color: #0000FF;">Load</span><span style="color: #008000;">&#40;</span>Server<span style="color: #008000;">.</span><span style="color: #0000FF;">MapPath</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;App_Data<span style="color: #008080; font-weight: bold;">\\</span>News.xml&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
 XElement notizia<span style="color: #008000;">=</span> newsXML<span style="color: #008000;">.</span><span style="color: #0000FF;">XPathSelectElement</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;News/Notizia[@Id = '&quot;</span> <span style="color: #008000;">+</span> _idNews <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;']&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
 notizia<span style="color: #008000;">.</span><span style="color: #0000FF;">Element</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Title&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Nuovo Titolo&quot;</span><span style="color: #008000;">;</span>
 notizia<span style="color: #008000;">.</span><span style="color: #0000FF;">Element</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Text&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Nuovo testo&quot;</span><span style="color: #008000;">;</span>
 notizia<span style="color: #008000;">.</span><span style="color: #0000FF;">Element</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Data&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;31/12/2010&quot;</span><span style="color: #008000;">;</span>
 notizia<span style="color: #008000;">.</span><span style="color: #0000FF;">Element</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Evidence&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;False&quot;</span><span style="color: #008000;">;</span>
 newsXML<span style="color: #008000;">.</span><span style="color: #0000FF;">Save</span><span style="color: #008000;">&#40;</span>Server<span style="color: #008000;">.</span><span style="color: #0000FF;">MapPath</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;App_Data<span style="color: #008080; font-weight: bold;">\\</span>News.xml&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p class="buymebeer"><form action="https://www.paypal.com/cgi-bin/webscr" target="paypal" method="post"><input type="hidden" name="cmd" value="_xclick" /><input type="hidden" name="business" value="antferr@gmail.com" /><input type="hidden" name="return" value="" /><input type="hidden" name="item_name" value="Buy Me a Beer for Linq to Xml – gestione news (part 2)" /><input type="hidden" name="amount" value="" /><input type="image" src="http://www.antferr.com/wp-content/plugins/buy-me-beer/icon_cafe.gif" align="left" alt="Mi offri un caffè?" title="Mi offri un caffè?" hspace="3" /></form><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&amp;business=antferr@gmail.com&amp;amount=&amp;return=&amp;item_name=Buy+Me+a+Beer+for+Linq+to+Xml+–+gestione+news+(part+2)" target="paypal">Se i miei post ti sono stati di aiuto o ti sono sembrati intressanti puoi offrirmi un caffè :)</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.antferr.com/aspnet/linq-to-xml-gestione-news-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Linq to Xml – gestione news (part 1)</title>
		<link>http://www.antferr.com/aspnet/linq-to-xml-gestione-news-part-1/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=linq-to-xml-gestione-news-part-1</link>
		<comments>http://www.antferr.com/aspnet/linq-to-xml-gestione-news-part-1/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 09:57:09 +0000</pubDate>
		<dc:creator>antferr</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.antferr.com/?p=501</guid>
		<description><![CDATA[
			
				
			
		
Linq è molto semplice da imparare e nel contempo molto potente. Linq dispone di quattro implementazioni: Linq to Sql, Linq to Entities, Linq to DataSets, Linq to Object e Linq to XML. In questo post cercherò di implementare un piccolo sistema di gestione delle news basato su Linq  [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.antferr.com%2Faspnet%2Flinq-to-xml-gestione-news-part-1%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.antferr.com%2Faspnet%2Flinq-to-xml-gestione-news-part-1%2F&amp;source=antferr&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Linq è molto semplice da imparare e nel contempo molto potente. Linq dispone di quattro implementazioni: Linq to Sql, Linq to Entities, Linq to DataSets, Linq to Object e Linq to XML. In questo post cercherò di implementare un piccolo sistema di gestione delle news basato su Linq to Xml.<br />
Partiamo dalla nostra sorgente dati, ovvero dal file XML. Esso è composto dal primo elemento &#8220;News&#8221; all&#8217;interno del quale sono contenute tutte le &#8220;Notizia&#8221;. Ogni notizia possiede un attributo identificativo &#8220;Id&#8221; che corrisponde alla data trasformata in stringa secondo la convenzione <em>yyyyMMddhhmmss</em>. Le notizie inoltre possiedono l&#8217;elemento &#8220;Data&#8221; che rappresenta la data dell&#8217;evento per cui è stata creata la news e un elemento &#8220;Evidence&#8221; per identificare le news più importanti.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><span style="color: #99cc00"></span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p501code13'); return false;">Mostra codice</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p50113"><td class="code" id="p501code13"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;News<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Notizia</span> <span style="color: #000066;">Id</span>=<span style="color: #ff0000;">&quot;20100707102340&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>My first new<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Text<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>My text,go on antferr.com<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Text<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>07/07/2010<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Evidence<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>True<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Evidence<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Notizia<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Notizia</span> <span style="color: #000066;">Id</span>=<span style="color: #ff0000;">&quot;20100706102340&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>My second new<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Text<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Hello readers<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Text<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>06/07/2010<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Evidence<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>True<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Evidence<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Notizia<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/News<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>
<!-- Begin Google Adsense code -->
<script type="text/javascript"><!--
google_ad_client = "pub-6474961034063636";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
google_ad_channel = "";
//-->
</script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
<br />
Fatto ciò non ci resta che creare la web application ASP.NET C#, ipotizzando di inserire il file xml nella directory App_Data e di chiamarlo semplicemente News.xml<br />
Partiamo dal backend, cioè dalla parte amministrativa che deve implementare le funzionalità di inserimento, modifica e cancellazione delle news.</p>
<h4>Inserimento</h4>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><span style="color: #99cc00"></span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p501code14'); return false;">Mostra codice</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p50114"><td class="code" id="p501code14"><pre class="csharp" style="font-family:monospace;">XDocument newsXML <span style="color: #008000;">=</span> XDocument<span style="color: #008000;">.</span><span style="color: #0000FF;">Load</span><span style="color: #008000;">&#40;</span>Server<span style="color: #008000;">.</span><span style="color: #0000FF;">MapPath</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;App_Data<span style="color: #008080; font-weight: bold;">\\</span>News.xml&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                XElement elemento <span style="color: #008000;">=</span> newsXML<span style="color: #008000;">.</span><span style="color: #0000FF;">Element</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;News&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                var notizia <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> XElement<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Notizia&quot;</span>, <span style="color: #008000;">new</span> XAttribute<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Id&quot;</span>, news<span style="color: #008000;">.</span><span style="color: #0000FF;">Id</span><span style="color: #008000;">&#41;</span>,
                    <span style="color: #008000;">new</span> XElement<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Title&quot;</span>, <span style="color: #666666;">&quot;Il mio titolo&quot;</span><span style="color: #008000;">&#41;</span>,
                    <span style="color: #008000;">new</span> XElement<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Text&quot;</span>, <span style="color: #666666;">&quot;Il mio testo&quot;</span><span style="color: #008000;">&#41;</span>,
                    <span style="color: #008000;">new</span> XElement<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Data&quot;</span>, <span style="color: #666666;">&quot;10/07/2010&quot;</span><span style="color: #008000;">&#41;</span>,
                    <span style="color: #008000;">new</span> XElement<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Evidence&quot;</span>, <span style="color: #666666;">&quot;True&quot;</span><span style="color: #008000;">&#41;</span>
                        <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                elemento<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>notizia<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                newsXML<span style="color: #008000;">.</span><span style="color: #0000FF;">Save</span><span style="color: #008000;">&#40;</span>Server<span style="color: #008000;">.</span><span style="color: #0000FF;">MapPath</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;App_Data<span style="color: #008080; font-weight: bold;">\\</span>News.xml&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>Per far sì che il codice sopra funzioni correttamente è necessario aggiungere i riferimenti a System.Xml.Linq e System.Xml.XPath. Il funzionamento è molto semplice. Creiamo un oggetto di tipo XDocument all&#8217;interno del quale carichiamo il contenuto del nostro file xml, poi creiamo un oggetto di tipo XElement che conterrà il contenuto del tag <News>. A questo punto non ci resta che creare una nuova <Notizia> come un nuovo XElement da aggiungere sotto il tag <News> e salvare il file. Ovviamente il contenuto della news non dovrebbe essere cablato come in questo esempio ma derivante da input inseriti dall&#8217;amministratore.</p>
<p class="buymebeer"><form action="https://www.paypal.com/cgi-bin/webscr" target="paypal" method="post"><input type="hidden" name="cmd" value="_xclick" /><input type="hidden" name="business" value="antferr@gmail.com" /><input type="hidden" name="return" value="" /><input type="hidden" name="item_name" value="Buy Me a Beer for Linq to Xml – gestione news (part 1)" /><input type="hidden" name="amount" value="" /><input type="image" src="http://www.antferr.com/wp-content/plugins/buy-me-beer/icon_cafe.gif" align="left" alt="Mi offri un caffè?" title="Mi offri un caffè?" hspace="3" /></form><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&amp;business=antferr@gmail.com&amp;amount=&amp;return=&amp;item_name=Buy+Me+a+Beer+for+Linq+to+Xml+–+gestione+news+(part+1)" target="paypal">Se i miei post ti sono stati di aiuto o ti sono sembrati intressanti puoi offrirmi un caffè :)</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.antferr.com/aspnet/linq-to-xml-gestione-news-part-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

