2 Eylül 2012 Pazar

0 Xml Tagı İçindeki Veriyi Html Taglarından Temizlemek

Windows 8 için Rss Reader yapmakla uğraşırken karşılaştığım bir problem vardı. Rss çekmek için kullandığım sitenin description tagının içerisinde bolca html tagı vardı bu da ekranda göstermek istediğim veriyi kirli bir hale getiriyordu.Bununla ilgili yaptığım araştırmalar sonucu şöyle bir kod satırına denk geldim,paylaşmak istedim.Ben faydasını gördüm sizin de işinize yarar umarım:


public Item(XElement element)//
        {
         ....
       
            this.Description = element.Element("description").Value;
            Description = Regex.Replace(Description, "<[^>]*>", string.Empty);
             this.Description = Description;

            .....
                    
        }
Kırmızı ile çizili kod satırı sayesinde xml tagı olan description içerisindeki şu tarz bir veri:


<description>
<p><strong>This Month in Photo of the Day: <a href="http://environment.nationalgeographic.com/environment/habitats/?source=podtheme">Nature</a> and <a href="http://environment.nationalgeographic.com/environment/natural-disasters/?source=podtheme">Weather</a> Photos</strong></p> <p>In the garden </p> <p><em>(This photo and caption were submitted to My Shot.)</em></p> <br /> <p><a href="http://photography.nationalgeographic.com/photography/photos/patterns-flora/?source=podrelated">Download flower wallpapers &raquo; </a> <br/> <a href="http://photography.nationalgeographic.com/photography/photo-tips/nature-landscape-photos/?source=podrelated">Get tips on photographing nature &raquo;</a> </p><br clear="both" style="clear: both;"/><a href="http://photography.nationalgeographic.com/photography/photo-of-the-day/tulips-geraniums/"><img src="http://images.nationalgeographic.com/exposure/core_media/ngphoto/image/58702_0_360x270.jpg"/></a><img src="http://feeds.feedburner.com/~r/ng/photography/photo-of-the-day/~4/CjK8YTwcmGE" height="1" width="1"/>
</description>

Aşağıdaki hale geldi:

This Month in Photo of the Day:Nature and weather Photos

In the Garden 

(This photo and caption were submitted to My Shot.)

Download flower wallpapers &raquo;

Get tips on photographing nature &raquo;



** Yazmış olduğumuz bu tek satırlık kod ile html taglarıyla ilgili herhangi bir işaret varsa,verimizi bunlardan arındırmış olduk.Xml tagı içindeki veriyi html taglarından kolayca temizlemiş olduk.


Not: Regex Fonksiyonu için, ctrl ve . ya aynı anda basarsanız otomatik olarak gerekli kütüphaneyi ekleyecektir(Visual Studio 2012 C# kısayolu) ya da manuel olarak using System.Text.RegularExpressions; kütüphanesini eklemeniz de yeterli olacaktır.


İyi Çalışmalar:)

0 yorum:

Yorum Gönder