michelc Blog

Just <strong>another</strong> WordPress.com weblog

Archive for the ‘Code Snippets’ Category

VS2003 – Echec de l’actualisation du projet

leave a comment »

Après avoir fermé toutes les session de Visual Studio, Passer dans l’explorateur de fichier et aller dans le sous-répertoire :

  • – Documents and Settings
  • – – « userlogin » (login de l’utilisateur)
  • – – – VSWebCache
  • – – – – « hostname » (nom de la machine)

Et supprimer le cache du projet qui pose problème (ou éventuellement de tous les projets).

(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/1937)

Written by michel

19 avril 2006 at 9:52

Publié dans .net, Code Snippets

Throwing from XSLT

leave a comment »

Vu sur http://weblogs.asp.net/george_v_reilly/archive/2006/03/01/439402.aspx

I (George V. Reilly, not me) needed to add some declarative error checking to some XSLT templates recently. Specifically, I wanted to throw an error if my selects yielded an empty string, indicating that the input XML was wrong.

Unfortunately, there seems to be no easy way of doing this in XSLT, nor in XslTransform. The approved way is to validate against an XSD schema, but for various reasons, I didn’t want to go to the hassle of creating one.

I found a partial solution using xsl:message with the terminate= »yes » attribute. Under XslTransform.Transform() the following code throws an exception if the XPath expression is empty.

<xsl:if test="not(/some/xpath/expression)">
   <xsl:message terminate="yes">Missing expression</xsl:message>
</xsl:if>
<xsl:value-of select="/some/xpath/expression" />

It doesn’t do anything, however, in XMLSpy.

The downside, of course, is that you have to maintain the expression in two places, and the template becomes littered with those annoying tests.

(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/1613)

Written by michel

2 mars 2006 at 7:27

Publié dans Code Snippets, xml

HTTP 301 – Moved Permanently

with one comment

<script runat="server">
private void Page_Load(object sender, System.EventArgs e) {
  Response.Status = "301 Moved Permanently";
  Response.AddHeader("Location", "http://www.new-url.com");
}
</script>

(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/1295)

Written by michel

24 janvier 2006 at 1:13

Blogmarks to Html conversion

with one comment

Ce fichier permet de transformer le fil atom de Blogmarks (par exemple http://api.blogmarks.net/user/ms_michel) en code html afin de pouvoir l’intégrer à une page web.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:atom="http://purl.org/atom/ns#draft-ietf-atompub-format-05" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <xsl:output method="html" />
  <xsl:template match="/">
    <xsl:apply-templates select="/atom:feed/atom:head" mode="before" />
    <xsl:apply-templates select="/atom:feed/atom:entry" />
    <xsl:apply-templates select="/atom:feed/atom:head" mode="after" />
  </xsl:template>
  <xsl:template match="atom:feed/atom:head" mode="before" >
    <!-- <h3><xsl:value-of select="atom:title" /></h3> -->
  </xsl:template>
  <xsl:template match="atom:feed/atom:head" mode="after">
    <p><a href="{atom:link[@rel='alternate']/@href}"><img src="http://blogmarks.net/img/88x31_neg.png" alt="blogmarks.net" /></a></p>
  </xsl:template>
  <xsl:template match="atom:feed/atom:entry">
    <div>
      <xsl:choose>
        <xsl:when test="position() mod 2 = 1">
          <xsl:attribute name="class">bm_blogmarks bm_odd</xsl:attribute>
        </xsl:when>
        <xsl:otherwise>
          <xsl:attribute name="class">bm_blogmarks bm_even</xsl:attribute>
        </xsl:otherwise>
      </xsl:choose>
      <a href="{atom:link[@rel='related']/@href}"><img src="{atom:link[@rel='image']/@href}" alt="" /></a>
      <h4><a href="{atom:link[@rel='related']/@href}"><xsl:value-of select="atom:title" /></a></h4>
      <p><xsl:value-of select="atom:summary" disable-output-escaping="yes" /></p>
      <p class="blogmarks-tags">
        <xsl:value-of select="substring(atom:published, 0, 11)" />
        <xsl:if test="atom:category">
          <xsl:for-each select="atom:category">
            <xsl:text> - </xsl:text><a href="{@term}{@sheme}"><xsl:value-of select="@label" /></a>
          </xsl:for-each>
        </xsl:if>
      </p>
    </div>
  </xsl:template>
</xsl:stylesheet>

Le code html généré est inspiré par celui de Hot Links. Il peut ensuite être présenté grâce à la feuille de style CSS suivante :

.bm_blogmarks {
  margin: 10px auto;
  padding: 1%;
  background-color: #f5f5f5;
  border: 1px solid #d9d9d9;
  width: 97%;
  overflow:auto;
}
.bm_even {
  background-color: #fcfcfc;
}
.bm_blogmarks h4 {
  margin-top: 0;
}
.bm_blogmarks p.bm_tags {
  margin-bottom: 0;
  display: block;
  clear: left;
}
.bm_blogmarks img {
  margin: 0 0px 5px 10px;
  float: right;
  border: 0;
  clear: none;
  width: 144px;
  height: 107px;
}
.bm_even img {
  margin: 0 10px 5px 0px;
  float: left;
}

(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/1206)

Written by michel

19 janvier 2006 at 8:18

Publié dans Code Snippets, html, xml

RDF to Html conversion

with one comment

Ce fichier permet de transformer un fil RDF en code html afin de pouvoir l’intégrer à une page web.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:foo="http://purl.org/rss/1.0/">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <xsl:apply-templates select="/rdf:RDF/foo:channel"/>
    </xsl:template>
    <xsl:template match="/rdf:RDF/foo:channel">
        <h3><xsl:value-of select="foo:title"/></h3>
        <p><xsl:value-of select="foo:description"/></p>
        <ul>
            <xsl:apply-templates select="/rdf:RDF/foo:item"/>
        </ul>
    </xsl:template>
    <xsl:template match="/rdf:RDF/foo:item">
        <li>
            <a href="{foo:link}" title="{substring(dc:date, 0, 11)}"><xsl:value-of select="foo:title"/></a>
            <p><xsl:value-of select="foo:description" disable-output-escaping="yes" /></p>
        </li>
    </xsl:template>
</xsl:stylesheet>

(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/1164)

Written by michel

16 janvier 2006 at 3:04

Publié dans Code Snippets, html, xml

RSS to Html conversion

leave a comment »

Ce fichier permet de transformer un fil RSS (version 0.9x ou 2.0) en code html afin de pouvoir l’intégrer à une page web.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:wfw="http://wellformedweb.org/CommentAPI/">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <xsl:apply-templates select="/rss/channel"/>
    </xsl:template>
    <xsl:template match="/rss/channel">
        <h3><xsl:value-of select="title"/></h3>
        <p><xsl:value-of select="description"/></p>
        <ul>
            <xsl:apply-templates select="item"/>
        </ul>
    </xsl:template>
    <xsl:template match="/rss/channel/item">
        <li>
            <a href="{link}" title="{substring(pubDate, 0, 11)}"><xsl:value-of select="title"/></a>
            <p><xsl:value-of select="description" disable-output-escaping="yes" /></p>
        </li>
    </xsl:template>
</xsl:stylesheet>

(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/1163)

Written by michel

16 janvier 2006 at 3:03

Publié dans Code Snippets, html, xml

Atom to Html conversion

with one comment

Ce fichier permet de transformer un fil atom en code html afin de pouvoir l’intégrer à une page web.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
  xmlns:atom="http://purl.org/atom/ns#"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:dc="http://purl.org/dc/elements/1.1/">
    <xsl:output method="html"/>
    <xsl:template match="/">
    <xsl:apply-templates select="/atom:feed/atom:head"/>
        <xsl:apply-templates select="/atom:feed"/>
    </xsl:template>
    <xsl:template match="atom:feed/atom:head">
        <h3><xsl:value-of select="atom:title"/></h3>
        <xsl:if test="atom:tagline"><p><xsl:value-of select="atom:tagline"/></p></xsl:if>
        <xsl:if test="atom:subtitle"><p><xsl:value-of select="atom:subtitle"/></p></xsl:if>
    </xsl:template>
    <xsl:template match="/atom:feed">
        <h3><xsl:value-of select="atom:title"/></h3>
        <xsl:if test="atom:tagline"><p><xsl:value-of select="atom:tagline"/></p></xsl:if>
        <xsl:if test="atom:subtitle"><p><xsl:value-of select="atom:subtitle"/></p></xsl:if>
        <ul>
            <xsl:apply-templates select="atom:entry"/>
        </ul>
    </xsl:template>
    <xsl:template match="atom:entry">
        <li>
            <a href="{atom:link[@rel='related']/@href}" title="{substring(atom:published, 0, 11)}"><xsl:value-of select="atom:title"/></a>
            <xsl:choose>
                <xsl:when test="atom:content != ''">
                    <p><xsl:value-of select="atom:content" disable-output-escaping="yes" /></p>
                </xsl:when>
                <xsl:otherwise>
                    <p><xsl:value-of select="atom:summary" disable-output-escaping="yes" /></p>
                </xsl:otherwise>
            </xsl:choose>
        </li>
    </xsl:template>
</xsl:stylesheet>

(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/1162)

Written by michel

16 janvier 2006 at 2:59

Publié dans Code Snippets, html, xml

Comment résoudre le problème du « double-clic » en asp.net

with one comment

Une méthode générique pour pallier au problème du « double-clic » quand un utilisateur double clique au lieu de simplement cliquer une fois pour valider un formulaire, ce qui risque de le soumettre 2 fois. Pour contourner ça, le bouton qui a été cliqué est caché une fois que l’utilisateur a cliqué dessus.

Pour que ça fonctionne avec tous les formulaires, la bidouille est appliqué au niveau de l’évènement Render de la page.

protected override void Render(HtmlTextWriter output) {
    // Get normal html ouput
    StringBuilder stringBuilder = new StringBuilder();
    StringWriter stringWriter = new StringWriter(stringBuilder);
    HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
    base.Render(htmlWriter);
    string html = stringBuilder.ToString();
    // Enhance submit buttons
    string onclick1 = "\"if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate();";
    string onclick2 = "\"this.style.display='none';";
    html = html.Replace("onclick=" + onclick1, "onclick=" + onclick2 + onclick1.Substring(1));
    // Render updated html
    output.Write(html);
}

(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/975)

Written by michel

14 décembre 2005 at 7:32

Publié dans .net, Code Snippets

Générer GoogleSearchService.dll à partir de GoogleSearch.wsdl

leave a comment »

Pour générer la source C#, taper :

wsdl GoogleSearch.wsdl

Cele crée un fichier GoogleSearchService.cs, à compiler par :

csc /target:library GoogleSearchService.cs

Ou pour Mono par :

mcs /target:library GoogleSearchService.cs

Ce qui produit l’assembly : GoogleSearchService.dll
(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/976)

Written by michel

14 décembre 2005 at 8:29

Publié dans .net, Code Snippets

Un formulaire simple pour tester l’envoi de mail

leave a comment »

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Mail" %>
<script runat="server">    
void btnSubmit_Click(Object sender, EventArgs e) {
  MailMessage mail = new MailMessage();
  mail.To = txtTo.Text;
  mail.From = txtFrom.Text;
  mail.Subject = txtSubject.Text;
  mail.Body = txtMessage.Text;
  mail.Priority = MailPriority.High;
  mail.BodyFormat = MailFormat.Text;
  SmtpMail.SmtpServer = txtSmtpServer.Text;
  if (txtSmtpUsername.Text.Trim() != "") {
    if (txtSmtpPassword.Text.Trim() != "") {
      mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
      mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", txtSmtpUsername.Text);
      mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", txtSmtpPassword.Text);
    }
  }
  try {
    SmtpMail.Send(mail);
    Response.Write("OK!");
  } catch (Exception ex) {
    Response.Write("KO: " + ex.ToString());
  }
}
</script>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Mail test</title>
  </head>
  <body>
    <form runat="server">
      <ul>
        <li>Smtp Server : <asp:TextBox id="txtSmtpServer" runat="server"></asp:TextBox></li>
        <li>Smtp Username : <asp:TextBox id="txtSmtpUsername" runat="server"></asp:TextBox></li>
        <li>Smtp Password : <asp:TextBox id="txtSmtpPassword" runat="server"></asp:TextBox></li>
        <li>From : <asp:TextBox id="txtFrom" runat="server"></asp:TextBox></li>
        <li>To : <asp:TextBox id="txtTo" runat="server"></asp:TextBox></li>
        <li>Subject : <asp:TextBox id="txtSubject" runat="server"></asp:TextBox></li>
        <li>Message : <asp:TextBox id="txtMessage" TextMode="MultiLine" runat="server"></asp:TextBox></li>
      </ul>
      <asp:Button runat="server" id="btnSubmit" OnClick="btnSubmit_Click" Text="Send"></asp:Button>
    </form>
  </body>
</html>

(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/906)

Written by michel

23 novembre 2005 at 4:30

Publié dans .net, Code Snippets