如何添加CSS参考.NET SyndicationFeed? [英] How to Add CSS Reference to .NET SyndicationFeed?

查看:218
本文介绍了如何添加CSS参考.NET SyndicationFeed?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的SyndicationFeed与许多SyndicationItems。每个SyndicationItem都有HtmlContent。 HtmlContent是一个表。这显示确定,但现在我需要在表上最少的样式/填充。我相信我需要添加样式表引用

 <?xml-stylesheet type =text / csshref = http://you.com/rss.css?> 

但我不知道如何使用.NET 4.5 SyndicationFeed或Atom10FeedFormatter类。 p>

这里是由WCF服务生成的(伪)代码。

  var feed = new feed(......) 
var xqn = new XmlQualifiedName(mysys,http://www.w3.org/2000/xmlns/);
feed.AttributeExtensions.Add(xqn,myuri);
feed.ElementExtensions.Add(creator,sys,teaminfo);
feed.Authors.Add(new SyndicationPerson(emailaddr));
feed.Categories.Add(new SyndicationCategory(mycategory));
feed.Description = new TextSyndicationContent(feedDesc);
feed.Copyright = SyndicationContent.CreatePlaintextContent(copyr);
feed.Language =en-us;
feed.ImageUrl = new Uri(myuri +/ logo);
var dbitems = GetListFromDb(key);
var syndItems = new List< synitem>();
foreach(dbitems中的var项)
{
var htmlContent =
SyndicationContent.CreateHtmlContent(MakeHtmlFromStuff(item));
var itemUri = new Uri(myuri + itemuri);
var synitem = new synitem(item.code,htmlContent,itemUri,item.key,DateTimeOffset.UtcNow);
syndItems.Add(synitem);
}
feed.Items = syndItems;
return new Atom10FeedFormatter(feed);

希望不会遗漏任何明显的东西。



感谢。

解决方案


我相信我需要添加一个样式表引用

 <?xml-stylesheet type =text / css href =http://you.com/rss.css?> 

但我不知道如何使用.NET 4.5 SyndicationFeed或
Atom10FeedFormatter



这里是一个完整的代码示例: b

 使用System; 
using System.Collections.Generic;
using System.ServiceModel.Syndication;
using System.Xml;

命名空间FeedProcessing
{
类FeedProcessing
{
static void Main(string [] args)
{
FeedProcessing fp = new FeedProcessing();
fp.CreateFeed();
}
public void CreateFeed()
{
SyndicationFeed feed = new SyndicationFeed(Feed Title,Feed Description,new Uri(http:// Feed / Alternate / Link),FeedID,DateTime.Now);
//添加自定义属性。
XmlQualifiedName xqName = new XmlQualifiedName(CustomAttribute);
feed.AttributeExtensions.Add(xqName,Value);

SyndicationPerson sp = new SyndicationPerson(jesper@contoso.com,Jesper Aaberg,http:// Jesper / Aaberg);
feed.Authors.Add(sp);

SyndicationCategory category = new SyndicationCategory(FeedCategory,CategoryScheme,CategoryLabel);
feed.Categories.Add(category);

feed.Contributors.Add(new SyndicationPerson(lene@contoso.com,Lene Aaling,http:// lene / aaling));
feed.Copyright = new TextSyndicationContent(Copyright 2007);
feed.Description = new TextSyndicationContent(This is a sample feed);

//添加自定义元素。
XmlDocument doc = new XmlDocument();
XmlElement feedElement = doc.CreateElement(CustomElement);
feedElement.InnerText =一些文本;
feed.ElementExtensions.Add(feedElement);

feed.Generator =示例代码;
feed.Id =FeedID;
feed.ImageUrl = new Uri(http://server/image.jpg);

TextSyndicationContent textContent = new TextSyndicationContent(Some text content);
SyndicationItem item = new SyndicationItem(Item Title,textContent,new Uri(http:// server / items),ItemID,DateTime.Now);

List< SyndicationItem> items = new List< SyndicationItem>();
items.Add(item);
feed.Items = items;

feed.Language =en-us;
feed.LastUpdatedTime = DateTime.Now;

SyndicationLink link = new SyndicationLink(new Uri(http:// server / link),alternate,Link Title,text / html,1000);
feed.Links.Add(link);

XmlWriter atomWriter = XmlWriter.Create(atom.xml);
atomWriter.WriteProcessingInstruction(xml-stylesheet,
type ='text / css'href ='http://you.com/rss.css');
Atom10FeedFormatter atomFormatter = new Atom10FeedFormatter(feed);
atomFormatter.WriteTo(atomWriter);
atomWriter.Close();

XmlWriter rssWriter = XmlWriter.Create(rss.xml);
rssWriter.WriteProcessingInstruction(xml-stylesheet,
type ='text / css'href ='http://you.com/rss.css');
Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(feed);
rssFormatter.WriteTo(rssWriter);
rssWriter.Close();
}
}
}



 <?xml version =1.0encoding =utf-8?> 
<?xml-stylesheet type ='text / css'href ='http://you.com/rss.css'?>
< feed xml:lang =en-usCustomAttribute =Valuexmlns =http://www.w3.org/2005/Atom>
< title type =text> Feed标题< / title>
< subtitle type =text>这是一个示例Feed< / subtitle>
< id> FeedID< / id>
< rights type =text>版权所有2007< / rights>
< updated> 2012-12-28T19:50:18-08:00< / updated>
< category term =FeedCategorylabel =CategoryLabelscheme =CategoryScheme/>
< logo> http://server/image.jpg< / logo>
< author>
< name> Jesper Aaberg< / name>
< uri> http:// Jesper / Aaberg< / uri>
< email> jesper@contoso.com< / email>
< / author>
< contributor>
< name> Lene Aaling< / name>
< uri> http:// lene / aaling< / uri>
< email> lene@contoso.com< / email>
< / contributor>
< generator>示例代码< / generator>
< link rel =alternatehref =http:// feed / Alternate / Link/>
< link rel =alternatetype =text / htmltitle =Link Titlelength =1000href =http:// server / link/&
< CustomElement xmlns =>一些文本< / CustomElement>
< entry>
< id> ItemID< / id>
< title type =text>项目标题< / title>
< updated> 2012-12-28T19:50:18-08:00< / updated>
< link rel =alternatehref =http:// server / items/>
< content type =text>部分文字内容< / content>
< / entry>
< / feed>

rss.xml

 <?xml version =1.0encoding =utf-8?> 
<?xml-stylesheet type ='text / css'href ='http://you.com/rss.css'?>
< rss xmlns:a10 =http://www.w3.org/2005/Atomversion =2.0>
< channel CustomAttribute =Value>
< title> Feed标题< / title>
< link> http:// feed / Alternate / Link< / link>
< description>这是一个示例Feed< / description>
< language> en-us< / language>
< copyright>版权所有2007< / copyright>
< manageEditor> jesper@contoso.com< / manageEditor>
< lastBuildDate> Fri,28 Dec 2012 19:50:18 -0800< / lastBuildDate>
< category domain =CategoryScheme> FeedCategory< / category>
< generator>示例代码< / generator>
< a10:contributor>
< a10:name> Lene Aaling< / a10:name>
< a10:uri> http:// lene / aaling< / a10:uri>
< a10:email> lene@contoso.com< / a10:email>
< / a10:contributor>
< image>
< url> http://server/image.jpg< / url>
< title> Feed标题< / title>
< link> http:// feed / Alternate / Link< / link>
< / image>
< a10:id> FeedID< / a10:id>
< a10:link rel =alternatetype =text / htmltitle =Link Titlelength =1000href =http:// server / link/&
< CustomElement>一些文本< / CustomElement>
< item>
< guid isPermaLink =false> ItemID< / guid>
< link> http:// server / items< / link>
< title>项目标题< / title>
< description>一些文字内容< / description>
< a10:updated> 2012-12-28T19:50:18-08:00< / a10:updated>
< / item>
< / channel>
< / rss>

说明



每当 XmlWriter 实例,但在调用之前:

  atomFormatter。 WriteTo(atomWriter); 

或:

 code> rssFormatter.WriteTo(rssWriter); 

调用 WriteProcessingInstruction() 实例,传递两个必需的参数 - PI的名称(在这种情况下xml-stylesheet)和PI的文本code>type ='text / css'href ='http://you.com/rss.css')。


I have created a simple SyndicationFeed with many SyndicationItems. Each SyndicationItem has HtmlContent. The HtmlContent is a table. This displays OK, but now I need a minimum of styling/padding on the table. I believe I need to add a stylesheet reference like

<?xml-stylesheet type="text/css" href="http://you.com/rss.css" ?>

But I do not see how to do this with the .NET 4.5 SyndicationFeed or Atom10FeedFormatter classes.

Here is the (pseudo) code that is generated by a WCF service.

var feed = new feed(......);
var xqn = new XmlQualifiedName( "mysys" , "http://www.w3.org/2000/xmlns/" );
feed.AttributeExtensions.Add( xqn , myuri );
feed.ElementExtensions.Add( "creator" , "sys" , teaminfo );
feed.Authors.Add( new SyndicationPerson( emailaddr) );
feed.Categories.Add( new SyndicationCategory( mycategory ) );
feed.Description = new TextSyndicationContent( feedDesc );
feed.Copyright = SyndicationContent.CreatePlaintextContent(copyr);
feed.Language = "en-us";
feed.ImageUrl = new Uri( myuri + "/logo" );
var dbitems = GetListFromDb(key);
var syndItems = new List<synitem>();
foreach (var item in dbitems)
{
    var htmlContent =
        SyndicationContent.CreateHtmlContent( MakeHtmlFromStuff(item) );
    var itemUri = new Uri( myuri + itemuri );
    var synitem = new synitem( item.code , htmlContent , itemUri , item.key , DateTimeOffset.UtcNow );
    syndItems.Add( synitem);
}
feed.Items = syndItems;
return new Atom10FeedFormatter(feed);

Hopefully not missing something obvious. All ideas appreciated.

Thanks.

解决方案

I believe I need to add a stylesheet reference like

<?xml-stylesheet type="text/css" href="http://you.com/rss.css" ?>

But I do not see how to do this with the .NET 4.5 SyndicationFeed or Atom10FeedFormatter classes.

Here is a complete code example how to do this:

using System;
using System.Collections.Generic;
using System.ServiceModel.Syndication;
using System.Xml;

namespace FeedProcessing
{
    class FeedProcessing
    {
        static void Main(string[] args)
        {
            FeedProcessing fp = new FeedProcessing();
            fp.CreateFeed();
        }
        public void CreateFeed()
        {
            SyndicationFeed feed = new SyndicationFeed("Feed Title", "Feed Description", new Uri("http://Feed/Alternate/Link"), "FeedID", DateTime.Now);
            // Add a custom attribute.
            XmlQualifiedName xqName = new XmlQualifiedName("CustomAttribute");
            feed.AttributeExtensions.Add(xqName, "Value");

            SyndicationPerson sp = new SyndicationPerson("jesper@contoso.com", "Jesper Aaberg", "http://Jesper/Aaberg");
            feed.Authors.Add(sp);

            SyndicationCategory category = new SyndicationCategory("FeedCategory", "CategoryScheme", "CategoryLabel");
            feed.Categories.Add(category);

            feed.Contributors.Add(new SyndicationPerson("lene@contoso.com", "Lene Aaling", "http://lene/aaling"));
            feed.Copyright = new TextSyndicationContent("Copyright 2007");
            feed.Description = new TextSyndicationContent("This is a sample feed");

            // Add a custom element.
            XmlDocument doc = new XmlDocument();
            XmlElement feedElement = doc.CreateElement("CustomElement");
            feedElement.InnerText = "Some text";
            feed.ElementExtensions.Add(feedElement);

            feed.Generator = "Sample Code";
            feed.Id = "FeedID";
            feed.ImageUrl = new Uri("http://server/image.jpg");

            TextSyndicationContent textContent = new TextSyndicationContent("Some text content");
            SyndicationItem item = new SyndicationItem("Item Title", textContent, new Uri("http://server/items"), "ItemID", DateTime.Now);

            List<SyndicationItem> items = new List<SyndicationItem>();
            items.Add(item);
            feed.Items = items;

            feed.Language = "en-us";
            feed.LastUpdatedTime = DateTime.Now;

            SyndicationLink link = new SyndicationLink(new Uri("http://server/link"), "alternate", "Link Title", "text/html", 1000);
            feed.Links.Add(link);

            XmlWriter atomWriter = XmlWriter.Create("atom.xml");
            atomWriter.WriteProcessingInstruction("xml-stylesheet", 
                                                  "type='text/css' href='http://you.com/rss.css'");
            Atom10FeedFormatter atomFormatter = new Atom10FeedFormatter(feed);
            atomFormatter.WriteTo(atomWriter);
            atomWriter.Close();

            XmlWriter rssWriter = XmlWriter.Create("rss.xml");
            rssWriter.WriteProcessingInstruction("xml-stylesheet",
                                                 "type='text/css' href='http://you.com/rss.css'");
            Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(feed);
            rssFormatter.WriteTo(rssWriter);
            rssWriter.Close();
        }
    }
}

When this program is executed, two files are created:

atom.xml:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type='text/css' href='http://you.com/rss.css'?>
<feed xml:lang="en-us" CustomAttribute="Value" xmlns="http://www.w3.org/2005/Atom">
    <title type="text">Feed Title</title>
    <subtitle type="text">This is a sample feed</subtitle>
    <id>FeedID</id>
    <rights type="text">Copyright 2007</rights>
    <updated>2012-12-28T19:50:18-08:00</updated>
    <category term="FeedCategory" label="CategoryLabel" scheme="CategoryScheme" />
    <logo>http://server/image.jpg</logo>
    <author>
        <name>Jesper Aaberg</name>
        <uri>http://Jesper/Aaberg</uri>
        <email>jesper@contoso.com</email>
    </author>
    <contributor>
        <name>Lene Aaling</name>
        <uri>http://lene/aaling</uri>
        <email>lene@contoso.com</email>
    </contributor>
    <generator>Sample Code</generator>
    <link rel="alternate" href="http://feed/Alternate/Link" />
    <link rel="alternate" type="text/html" title="Link Title" length="1000" href="http://server/link" />
    <CustomElement xmlns="">Some text</CustomElement>
    <entry>
        <id>ItemID</id>
        <title type="text">Item Title</title>
        <updated>2012-12-28T19:50:18-08:00</updated>
        <link rel="alternate" href="http://server/items" />
        <content type="text">Some text content</content>
    </entry>
</feed>

and rss.xml:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type='text/css' href='http://you.com/rss.css'?>
<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
    <channel CustomAttribute="Value">
        <title>Feed Title</title>
        <link>http://feed/Alternate/Link</link>
        <description>This is a sample feed</description>
        <language>en-us</language>
        <copyright>Copyright 2007</copyright>
        <managingEditor>jesper@contoso.com</managingEditor>
        <lastBuildDate>Fri, 28 Dec 2012 19:50:18 -0800</lastBuildDate>
        <category domain="CategoryScheme">FeedCategory</category>
        <generator>Sample Code</generator>
        <a10:contributor>
            <a10:name>Lene Aaling</a10:name>
            <a10:uri>http://lene/aaling</a10:uri>
            <a10:email>lene@contoso.com</a10:email>
        </a10:contributor>
        <image>
            <url>http://server/image.jpg</url>
            <title>Feed Title</title>
            <link>http://feed/Alternate/Link</link>
        </image>
        <a10:id>FeedID</a10:id>
        <a10:link rel="alternate" type="text/html" title="Link Title" length="1000" href="http://server/link" />
        <CustomElement>Some text</CustomElement>
        <item>
            <guid isPermaLink="false">ItemID</guid>
            <link>http://server/items</link>
            <title>Item Title</title>
            <description>Some text content</description>
            <a10:updated>2012-12-28T19:50:18-08:00</a10:updated>
        </item>
    </channel>
</rss>

Explanation:

Whenever the XmlWriter instance is created, but before calling either:

atomFormatter.WriteTo(atomWriter);

or:

rssFormatter.WriteTo(rssWriter);

call the WriteProcessingInstruction() method on the respective XmlWriter instance, passing to it the two required parameters -- the name of the PI (in this case "xml-stylesheet") and the text of the PI (in this case "type='text/css' href='http://you.com/rss.css'").

这篇关于如何添加CSS参考.NET SyndicationFeed?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆