public class rss
{
public string title;
public string image_title;
public string image_link;
public string image_url;
public string description;
public string link;
public string language;
public string generator;
public string ttl;
public string copyright;
public string pubdate;
public string category;
public rss()
{}
public rss(string stitle,string simage_title,string simage_link,string simage_url,string sdescription,
string slink,string slanguage,string sgenerator,string sttl,string scopyright,string spubdate,string scategory)
{
title=stitle;
image_title=simage_title;
image_link=simage_link;
image_url=simage_url;
description=sdescription;
link=slink;
language=slanguage;
generator=sgenerator;
ttl=sttl;
copyright=scopyright;
pubdate=spubdate;
category=scategory;
}
public void create_xml(string filename,DataSet ds,string http)
{
XmlDocument doc = new XmlDocument(); //添加头部节点
XmlDeclaration decla = doc.CreateXmlDeclaration("1.0", "utf-8", "no");
doc.AppendChild(decla);
XmlElement stitle = doc.CreateElement("title");
stitle.InnerText = title;
XmlElement simage_title = doc.CreateElement("title");
simage_title.InnerText = image_title;
XmlElement simage_link = doc.CreateElement("link");
simage_link.InnerText = image_link;
XmlElement simage_url = doc.CreateElement("url");
simage_url.InnerText = image_url;
XmlElement sdescription = doc.CreateElement("description");
sdescription.InnerText = description;
XmlElement slink = doc.CreateElement("link");
slink.InnerText = link;
XmlElement slanguage = doc.CreateElement("language");
slanguage.InnerText = language;
XmlElement sgenerator = doc.CreateElement("generator");
sgenerator.InnerText = generator;
XmlElement sttl = doc.CreateElement("ttl");
sttl.InnerText = ttl;
XmlElement scopyright = doc.CreateElement("copyright");
scopyright.InnerText = copyright;
XmlElement spubdate = doc.CreateElement("pubdate");
spubdate.InnerText = pubdate;
XmlElement scategory = doc.CreateElement("category");
XmlElement image = doc.CreateElement("image");
XmlElement rss = doc.CreateElement("rss");
doc.AppendChild(rss);
XmlAttribute version = doc.CreateAttribute("version");
version.Value = "2.0";
rss.Attributes.Append(version);
XmlElement channel = doc.CreateElement("channel");
rss.AppendChild(channel);
channel.AppendChild(stitle);
channel.AppendChild(image);
image.AppendChild(simage_title);
image.AppendChild(simage_link);
image.AppendChild(simage_url);
channel.AppendChild(sdescription);
channel.AppendChild(slink);
channel.AppendChild(slanguage);
channel.AppendChild(sgenerator);
channel.AppendChild(sttl);
channel.AppendChild(scopyright);
channel.AppendChild(spubdate);
channel.AppendChild(scategory);
foreach (DataRow row in ds.Tables[0].Rows)
{
XmlElement item = doc.CreateElement("item");
XmlElement sstitle = doc.CreateElement("title");
sstitle.InnerText = row["title"].ToString();
XmlElement sslink = doc.CreateElement("link");
sslink.InnerText = http + row["id"].ToString();
XmlElement author=doc.CreateElement("author");
author.InnerText = row["username"].ToString();
XmlElement guid=doc.CreateElement("guid");
guid.InnerText = http + row["id"].ToString();
XmlElement sscategory=doc.CreateElement("category");
sscategory.InnerText = string.Empty;
XmlElement sspubdate=doc.CreateElement("pubdate");
sspubdate.InnerText=row["datetime"].ToString();
XmlElement comment=doc.CreateElement("comment");
comment.InnerText = string.Empty;
XmlElement ssdescription=doc.CreateElement("description");
ssdescription.InnerText=row["title"].ToString();
channel.AppendChild(item);
item.AppendChild(sstitle);
item.AppendChild(sslink);
item.AppendChild(author);
item.AppendChild(guid);
item.AppendChild(sscategory);
item.AppendChild(sspubdate);
item.AppendChild(comment);
item.AppendChild(ssdescription);
}
doc.Save(filename);
}
}
转载于:https://www.cnblogs.com/together/archive/2010/04/21/1717345.html