您的位置:首页 > 其它

关于XML的简单知识

2011-11-25 09:59 253 查看
如何直接从代码中使用RSS种子,使用LInq查询XML

XDocument xdoc =XDocument.Load(@"http://geekswithblogs.net/evjen/Rss.aspx");

var query = from rssFeed in xdoc.Descendants("channel")

select new

{

Title = rssFeed.Element("title").Value,

Description = rssFeed.Element("description").Value,

Link = rssFeed.Element("link").Value,

};

foreach (var item in query)

{

Console.WriteLine("TITLE: " + item.Title);

Console.WriteLine("DESCRIPTION: " + item.Description);

Console.WriteLine("LINK: " + item.Link);

}


  

把RSS添加到 ASP.NET MVC项目中

1.创建一个RssResult类继承抽象基类ActionResult

2.重写基类的ExecuteResult方法

3.ExecuteResult把ControllerContext传给调用者,使用它能得到数据和内容类型。

4.当你你把内容类型修改为rss类型时,你会想把数据序列化成rss(使用您自己的代码或其他的类库)和输出给响应正文。

5.在某个控制器中创建一个返回rss结果的action,设置返回类型为RssResult。从模型中获得你想输出的数据。

6.那么这个动作将会收到请求并返回任何你要的rss数据

public class RssActionResult : ActionResult

{

public SyndicationFeed Feed { get; set; }

public override void ExecuteResult(ControllerContext context)

{

context.HttpContext.Response.ContentType = "application/rss+xml";

Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(Feed);

using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output))

{

rssFormatter.WriteTo(writer);

}

}

}


  

<%@ Page ContentType="application/rss+xml" Language="C#" AutoEventWireup="true" CodeBehind="PostRSS.aspx.cs" Inherits="rr.web.Views.Blog.PostRSS" %><?xml version="1.0" encoding="utf-8"?>

<rss version="2.0">

<channel>

<title>ricky rosario's blog</title>

<link>http://<%= Request.Url.Host %></link>

<description>Blog RSS feed for rickyrosario.com</description>

<lastBuildDate><%= ViewData.Model.First().DatePublished.Value.ToUniversalTime().ToString("r") %></lastBuildDate>

<language>en-us</language>

<% foreach (Post p in ViewData.Model) { %>

<item>

<title><%= Html.Encode(p.Title) %></title>

<link>http://<%= Request.Url.Host + Url.Action("ViewPostByName", new RouteValueDictionary(new { name = p.Name })) %></link>

<guid>http://<%= Request.Url.Host + Url.Action("ViewPostByName", new RouteValueDictionary(new { name = p.Name })) %></guid>

<pubDate><%= p.DatePublished.Value.ToUniversalTime().ToString("r") %></pubDate>

<description><%= Html.Encode(p.Content) %></description>

</item>

<% } %>

</channel>

</rss>


  

另一个方式直接返回xml数据文本:

public ContentResult XMLData() {

StoryLink[] stories = GetAllStories();

XElement data = new XElement("StoryList", stories.Select(e => {

return new XElement("Story",

new XAttribute("title", e.Title),

new XAttribute("description", e.Description),

new XAttribute("link", e.Url));

}));

return Content(data.ToString(), "text/xml");

}


  

用来生成XML文本的StoryLink类定义如下:

public class StoryLink{

public string Title {get; set;}

public string Description { get; set; }

public string Url { get; set; }

}


  

操作方法返回的结果是一个XML片段:

<StoryList>

<Story title="First example story" description="This is the first example story"

link="/Story/1" />

<Story title="Second example story" description="This is the second example story"

link="/Story/2" />

<Story title="Third example story" description="This is the third example story"

link="/Story/3" />

</StoryList>


  

一下是关于XML的一些操作:

1.设置XML文档的特定值:

xdoc.Element("PLAY").Element("PERSONAE").

Element("PERSONA").SetValue("Bill Evjen, king of Denmark");


  

2.使用数据路输出XMl文档:

NorthwindDataContext dc = new NorthwindDataContext();

XElement xe = new XElement("Customer",

from c in dc.Customers

select new XElement("Customer",

new XElement("CustomerId", c.CustomerID),

new XElement("CompanyName", c.CompanyName),

new XElement("Country", c.Country),

new XElement("OrderNum", c.Orders.Count)));

xe.Save(@"C:\myCustomers.xml");


  

3.创建包含单个元素的对象

XElement xe = new XElement("Company",

new XElement("CompanyName", "Lipper"),

new XElement("CompanyAddress",

new XElement("Address", "123 Main Street"),

new XElement("City", "St. Louis"),

new XElement("State", "MO"),

new XElement("Country", "USA")));


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: