2015-01-31 58 views
2

我正在寻找为使用C#的网站创建RSS源。我试图接近,一旦我点击了一个按钮(“添加频道”),它想建立和节点元素,像这样:如何在使用C#的RSS提要中的通道元素内插入元素<item> ..</item>?

<item> 
<title> some title...here </title> 
<link> link to the article ...here </link> 
<description> article's description ...here </description> 
</item> 

这必须是<通道的孩子>元素。所以,到目前为止与我写的代码,它插入在RSS XML文件上述元件,但它确实它的<通道>元素以外,像这样:

<?xml version="1.0" encoding="utf-8"?> 
<rss version="2.0"> 
    <channel> 
    <title>Example Home Page</title> 
    <link>http://www.example.com</link> 
    <description>Educational Website...</description> 
    <image> 
     <url>http://www.example.com/images/logo.png</url> 
     <title>Example.com</title> 
     <link>http://www.example.com</link> 
    </image> 
    <category>Photography</category> 
    <language>en-us</language> 
    </channel> 
<item> 
<title> some title...here </title> 
<link> link to the article ...here </link> 
<description> article's description ...here </description> 
</item> 
</rss> 

这是代码:

XmlDocument doc = new XmlDocument(); 

    XmlNode item = doc.CreateElement("item"); 

    XmlNode Title = doc.CreateElement("title"); 
    Title.InnerText = TextBoxTitle.Text; 
    item.AppendChild(Title); 

    XmlNode link = doc.CreateElement("link"); 
    link.InnerText = "http://www.example.com/" + DropDownListCategory.SelectedItem.Text + ".aspx?key=" + TextBoxLink.Text + ".txt"; 
    item.AppendChild(link); 

    XmlNode description = doc.CreateElement("description"); 
    description.InnerText = TextBoxDescription.Text; 
    item.AppendChild(description); 

    doc.DocumentElement.AppendChild(item); 
    doc.Save(Server.MapPath("~/rss.xml")); 

我该怎么做?任何帮助或反馈将不胜感激。谢谢 !!!

回答

2

试试这个,如果我理解正确的要求:现在

XmlDocument doc = new XmlDocument(); 
    XmlNode rss = doc.CreateElement("rss"); 
    XmlAttribute version = doc.CreateAttribute("version"); 
    version.Value = "2.0"; 
    rss.Attributes.Append(version); 
    XmlNode channel = doc.CreateElement("channel"); 

    XmlNode item = doc.CreateElement("item"); 

    XmlNode Title = doc.CreateElement("title"); 
    Title.InnerText = "Title Text"; 
    item.AppendChild(Title); 

    XmlNode link = doc.CreateElement("link"); 
    link.InnerText = "http://www.example.com/.txt"; 
    item.AppendChild(link); 

    XmlNode description = doc.CreateElement("description"); 
    description.InnerText = "DESC"; 
    item.AppendChild(description); 

    channel.AppendChild(item); 
    rss.AppendChild(channel); 
    doc.AppendChild(rss); 
    doc.Save(Server.MapPath("~/rss.xml")); 

好了,我们得到了该文件已保存的地方,试试这个:

var xmldoc = new XmlDocument(); 
    xmldoc.Load(Server.MapPath("~/rss.xml")); 

    XmlNode channelNode = xmldoc.SelectSingleNode("descendant::channel"); 

    if (channelNode != null) 
    { 
    XmlNode item = xmldoc.CreateElement("item"); 

    XmlNode Title = xmldoc.CreateElement("title"); 
    Title.InnerText = "Title Text"; 
    item.AppendChild(Title); 

    XmlNode link = xmldoc.CreateElement("link"); 
    link.InnerText = "http://www.example.com/.txt"; 
    item.AppendChild(link); 

    XmlNode description = xmldoc.CreateElement("description"); 
    description.InnerText = "DESC"; 
    item.AppendChild(description); 

    channelNode.AppendChild(item); 
    } 
    doc.Save(Server.MapPath("~/rss.xml")); 
+0

到底追加大的东西的酷习惯。 – SimpleVar 2015-01-31 21:48:27

+0

谢谢你试图帮助我。我测试了你的代码,但是它做了我想要的,它在节点内部添加了元素。这种方法的问题在于,每次点击按钮添加新内容时,旧内容都将被删除。我喜欢一切都完好无损,只需添加新的 ....元素。就像我对待我的方法一样。我不正确的做法是在节点内添加这些元素。我不得不说,我已经手动创建了rss.xml结构,我唯一想编程的方法是添加节点。 – 2015-02-01 03:32:10

+0

@TomasJimenez请参阅上面的修改 – zaitsman 2015-02-01 05:17:26

1

.NET框架已建成为了支持在System.ServiceModel(您需要System.ServiceModel.dll的引用)中构建RSS订阅源,最好使用它来保证有效的RSS xml,并且更好地处理自己构造XML的工作。进一步阅读on MSDN

例如..

// read in the existing rss xml 
var rssFeedXml = XDocument.Load(@"c:\temp\rss3.xml"); // replace with server.mappath etc 
var feed = SyndicationFeed.Load(rssFeedXml.CreateReader());  
var items = new List<SyndicationItem>(feed.Items); 
feed.Items = items; 

// add the new item 
items.Add(
     new SyndicationItem(
     "some title...here ", 
     "some description here", 
     new Uri("http://example.come"))); 

// write the xml back out 
var rssFormatter = new Rss20FeedFormatter(feed, false); 
XDocument output = new XDocument(); 
using (var xmlWriter = output.CreateWriter()) 
    rssFormatter.WriteTo(xmlWriter); 

output.Save(@"c:\temp\rss.xml");