Search

just show me the code

Wednesday, November 5, 2008

asp.net handlers serving xml

your handler code
namespace XXXX.TourAdmin.Web.Admin
{
    /// 
    /// Summary description for $codebehindclassname$
    /// 
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class VideoHandler : IHttpHandler
    {
 
        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";
            context.Response.ContentType = "UTF-8";
            context.Response.Write(this.GetVideoXml());
        }
 
        protected string GetVideoXml()
        {
            VideoContext vc = new VideoContext();
            var videos = from v in vc.Videos
                         select new 
                         {
                             VideoClip = v.FileLocation,
                             Title = v.Title,
                             Date = v.VideoDate,
 
                         };
 
            XDocument xmlDoc = new XDocument();
            XElement xEleVideos = new XElement("videos");
            xmlDoc.Add(xEleVideos); 
            foreach (var v in videos)
            {
                xEleVideos.Add(new XElement("video",  new XAttribute("VideoClip", v.VideoClip),
                    new XAttribute("Title", v.Title), new XAttribute("Copy", v.Date)));
            }
            string s = xmlDoc.ToString();
            return s;
        }
 
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
here is the data:



TitleFileLocationVideoDateactive
Miami, Florida200809142009-02-12 00:00:001

now put the url in the address bar
http://localhost/XXXX.TourAdmin.Web/Admin/VideoHandler.ashx
<videos>
  <video VideoClip="20080914" Title="Miami, Florida" copy="2009-02-12T00:00:00" />
<videos>
Props to Lewis

No comments:

Post a Comment

Contributors