2010-10-16 7 views
1

J'ai un modèle où je veux remplacer certaines régions. Dans mon exemple ci-dessous, je veux extraire les régions entre les ... commentaires, les manipuler, puis les replacer après la manipulation.Extrait la région et les remplace pour un modèle

Je n'ai pas besoin de la logique pour fusionner les champs, mais j'ai besoin d'extraire les régions pour pouvoir utiliser ma logique et la replacer dans le gabarit.

Est-ce que quelqu'un connaît un moyen élégant ou simple d'extraire ces régions? J'espère aussi extraire les valeurs d'url dans le processus si c'est facile à faire en cours de route.

<table width="700" border="0" align="center" cellpadding="4" cellspacing="0"> 
<tr> 
    <td align="center" valign="top"> 
    <!--DynamicSlotStart url="http://www.test.com/itemdisplay0_10751_-1_57436_10001"--> 
    <table> 
    <tbody> 
    <tr> 
     <td><p><a title="[element='title']" href="[url]"><img border="0" alt="[element='title']" src="[element='photo' property='src' maxwidth='135']" width="135" height="135" /></a></p></td> 
    </tr> 
    <tr> 
     <td><span>[element='h1']</span></td> 
    </tr> 
    <tr> 
     <td><span><strong>[element='price']<br /> 
     </strong></span><span>[element='was_price']</span></td> 
    </tr> 
    <tr> 
     <td><span><a title="[element='title']" href="[url]">Details</a></span></td> 
    </tr> 
    </tbody> 
    </table> 
    <!--DynamicSlotFinish--> 
    </td> 
    <td align="center" valign="top"> 
    <!--DynamicSlotStart url="http://www.test.com/itemdisplay0_10751_-1_3379_10001"--> 
    <table> 
    <tbody> 
    <tr> 
     <td><p><a title="[element='title']" href="[url]"><img border="0" alt="[element='title']" src="[element='photo' property='src' maxwidth='135']" width="135" height="135" /></a></p></td> 
    </tr> 
    <tr> 
     <td><span>[element='h1']</span></td> 
    </tr> 
    <tr> 
     <td><span><strong>[element='price']<br /> 
     </strong></span><span>[element='was_price']</span></td> 
    </tr> 
    <tr> 
     <td><span><a title="[element='title']" href="[url]">Details</a></span></td> 
    </tr> 
    </tbody> 
    </table> 
    <!--DynamicSlotFinish--> 
    </td> 
    <td align="center" valign="top"> 
    <!--DynamicSlotStart url="http://www.test.com/itemdisplay0_10751_-1_104854_10001"--> 
    <table> 
    <tbody> 
    <tr> 
     <td><p><a title="[element='title']" href="[url]"><img border="0" alt="[element='title']" src="[element='photo' property='src' maxwidth='135']" width="135" height="135" /></a></p></td> 
    </tr> 
    <tr> 
     <td><span>[element='h1']</span></td> 
    </tr> 
    <tr> 
     <td><span><strong>[element='price']<br /> 
     </strong></span><span>[element='was_price']</span></td> 
    </tr> 
    <tr> 
     <td><span><a title="[element='title']" href="[url]">Details</a></span></td> 
    </tr> 
    </tbody> 
    </table> 
    <!--DynamicSlotFinish--> 
    </td> 
    <td align="center" valign="top"> 
    <!--DynamicSlotStart url="http://www.test.com/itemdisplay0_10751_-1_80977_10001"--> 
    <table> 
    <tbody> 
    <tr> 
     <td><p><a title="[element='title']" href="[url]"><img border="0" alt="[element='title']" src="[element='photo' property='src' maxwidth='135']" width="135" height="135" /></a></p></td> 
    </tr> 
    <tr> 
     <td><span>[element='h1']</span></td> 
    </tr> 
    <tr> 
     <td><span><strong>[element='price']<br /> 
     </strong></span><span>[element='was_price']</span></td> 
    </tr> 
    <tr> 
     <td><span><a title="[element='title']" href="[url]">Details</a></span></td> 
    </tr> 
    </tbody> 
    </table> 
    <!--DynamicSlotFinish--> 
    </td> 
</tr> 
</table> 

Répondre

0

Peut-être que ce projet sera utile: Html Agility Pack

Quel est exactement le pack Html Agility (de PAH)?

Il s'agit d'un analyseur HTML agile qui construit un DOM en lecture/écriture et prend en charge XPATH ou XSLT (vous n'avez pas besoin de comprendre XPATH ou XSLT pour l'utiliser, ne vous inquiétez pas ...). C'est une bibliothèque de code .NET qui vous permet d'analyser les fichiers HTML "hors du web". L'analyseur syntaxique est très tolérant avec le code HTML "réel" malformé. Le modèle objet est très similaire à ce que propose System.Xml, mais pour les documents HTML (ou les flux).

Html Agility Pack prend désormais en charge Linq to Objects (via une interface LINQ to Xml Like). Découvrez la nouvelle version bêta de jouer avec cette fonction

Exemples d'applications:

  • page fixation ou génération. Vous pouvez fixer une page comme vous le souhaitez, modifier le DOM, ajouter des nœuds, copier des nœuds, bien ... vous l'appelez.

  • Scanneurs Web. Vous pouvez facilement accéder à img/src ou a/hrefs avec des requêtes XPATH groupées.

  • Racleurs de bande. Vous pouvez facilement supprimer toute page Web existante dans un flux RSS par exemple, avec juste un fichier XSLT servant de lien. Un exemple est fourni.

Questions connexes