Search

just show me the code

Tuesday, November 4, 2008

Linq to xml

Linq (Language Integrated Query) works on xml too

<content>
  <gallery Name="All Videos">
    <video VideoClip="20081014.flv" Title="Orlando, Florida" Copy="Feb 12, 2009"/>
    <video VideoClip="20081121.flv" Title="Atlanta, Georgia" Copy="Feb 12, 2009"/>
  </gallery>
</content>



protected void LoadGallaryList()
{
    string xmlFile = this.Server.MapPath("~/Flash/_xml/video.xml"); 
    XDocument xmlDoc = XDocument.Load(xmlFile); 
    var videos = from v in xmlDoc.Descendants("video") // .Elements("video")
                 select new 
                 {
                     VideoClip = v.Attribute("VideoClip").Value,
                     Title = v.Attribute("Title").Value,
                     Date = v.Attribute("Copy").Value,
                 }; 
    foreach (var v in videos)
    {
        lblCurrent.Text += "title:" + v.Title + " VideoClip:" + v.VideoClip + " Date:" + v.Date + "<br/>";
    }
}

output:
title:Orlando, Florida VideoClip:20080914.flv Date:Feb 12, 2009
title:Atlanta, Georgia VideoClip:20081121.flv Date:Feb 12, 2009

No comments:

Post a Comment

Contributors