โดย jataz2 » 01/08/2011 1:09 pm
สร้างคลาส PRssList.cs
โค้ด: เลือกทั้งหมด
using System;
using System.Data;
using System.Configuration;
using System.Xml;
using System.Xml.Serialization;
namespace Revolic.Data.Rss.DTO
{
[XmlRoot(ElementName = "rss")]
public class PRssList
{
public PRssChannel channel;
public PRssList()
{
}
}
[XmlRoot(ElementName = "channel")]
public class PRssChannel
{
public string title = "";
public string link = "";
public string description = "";
public string language = "";
public string copyright = "";
[XmlElement(ElementName = "image")]
public PRssImage image;
[XmlElement(ElementName = "item")]
public PRssItem[] item;
public PRssChannel()
{
//
// TODO: Add constructor logic here
//
}
}
[XmlRoot(ElementName = "image")]
public class PRssImage
{
public string url = "";
public string title = "";
public string link = "";
public string width = "";
public string height = "";
public PRssImage()
{
}
}
[XmlRoot(ElementName = "item")]
public class PRssItem
{
[XmlAttribute(AttributeName="news_id")]
public string id = "";
public string title = "";
public string description = "";
public string link = "";
public string pubDate = "";
public string thumb = "";
public PRssItem()
{
}
}
}
ในหน้า Rss.aspx ก็หยอดค่าลงใน class แล้วเรีัยกใช้ serializer.Serialize() เพื่อ build เป็นโครงสร้าง xml
โค้ด: เลือกทั้งหมด
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Response.ContentType = "text/xml;charset=utf-8";
PRssList list = new PRssList();
PRssItem item = new PRssItem();
item.id = "508";
item.title = "xxxx";
item.link = "http://www.***.com/id,508/";
item.description = "Son Ho Young Son Ho Young";
item.pubDate = "17 Jul 2551";
PRssItem[] items = new PRssItem[2];
items[0] = item;
items[1] = item;
PRssImage image = new PRssImage();
image.title = "Freedom Of Music;";
image.url = "http://www.***.com/***/logo_default.jpg";
image.link = "http://www.***.com/";
PRssChannel channel = new PRssChannel();
channel.title = "Freedom Of Music;";
channel.link = "http://www.***.com/";
channel.description = "music,online,play,listen,vdo,clip,indies";
channel.language = "th-TH";
channel.copyright = "***.com";
channel.image = image;
channel.item = items;
list.channel = channel;
XmlSerializer serializer = new XmlSerializer(typeof(PRssList));
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty);
serializer.Serialize(Response.OutputStream, list, namespaces);
}
}
สร้างคลาส PRssList.cs
[code]using System;
using System.Data;
using System.Configuration;
using System.Xml;
using System.Xml.Serialization;
namespace Revolic.Data.Rss.DTO
{
[XmlRoot(ElementName = "rss")]
public class PRssList
{
public PRssChannel channel;
public PRssList()
{
}
}
[XmlRoot(ElementName = "channel")]
public class PRssChannel
{
public string title = "";
public string link = "";
public string description = "";
public string language = "";
public string copyright = "";
[XmlElement(ElementName = "image")]
public PRssImage image;
[XmlElement(ElementName = "item")]
public PRssItem[] item;
public PRssChannel()
{
//
// TODO: Add constructor logic here
//
}
}
[XmlRoot(ElementName = "image")]
public class PRssImage
{
public string url = "";
public string title = "";
public string link = "";
public string width = "";
public string height = "";
public PRssImage()
{
}
}
[XmlRoot(ElementName = "item")]
public class PRssItem
{
[XmlAttribute(AttributeName="news_id")]
public string id = "";
public string title = "";
public string description = "";
public string link = "";
public string pubDate = "";
public string thumb = "";
public PRssItem()
{
}
}
}[/code]
ในหน้า Rss.aspx ก็หยอดค่าลงใน class แล้วเรีัยกใช้ serializer.Serialize() เพื่อ build เป็นโครงสร้าง xml
[code]protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Response.ContentType = "text/xml;charset=utf-8";
PRssList list = new PRssList();
PRssItem item = new PRssItem();
item.id = "508";
item.title = "xxxx";
item.link = "http://www.***.com/id,508/";
item.description = "Son Ho Young Son Ho Young";
item.pubDate = "17 Jul 2551";
PRssItem[] items = new PRssItem[2];
items[0] = item;
items[1] = item;
PRssImage image = new PRssImage();
image.title = "Freedom Of Music;";
image.url = "http://www.***.com/***/logo_default.jpg";
image.link = "http://www.***.com/";
PRssChannel channel = new PRssChannel();
channel.title = "Freedom Of Music;";
channel.link = "http://www.***.com/";
channel.description = "music,online,play,listen,vdo,clip,indies";
channel.language = "th-TH";
channel.copyright = "***.com";
channel.image = image;
channel.item = items;
list.channel = channel;
XmlSerializer serializer = new XmlSerializer(typeof(PRssList));
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty);
serializer.Serialize(Response.OutputStream, list, namespaces);
}
}[/code]