如何创建一个自定义命名空间SyndicationFeed

如何创建一个自定义命名空间SyndicationFeed

本文介绍了如何创建一个自定义命名空间SyndicationFeed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能生成Atom提要将包含在下面的图片中显示的命名空间? Atom提要的所有节点都以答:

How can I generate Atom Feed which will contain the namespaces displayed in the image below? All the nodes of the Atom feed have to start with "a:".

下面是我在做什么,现在,但它不能正常工作。

Here is what I am doing right now, however it doesn't work.

    SyndicationFeed feed = new SyndicationFeed();
    XmlQualifiedName key = new XmlQualifiedName("os", "xmlns");
    feed.AttributeExtensions.Add(key, "http://a9.com/-/spec/opensearch/1.1/");

谢谢!

推荐答案

我相信它应该是

SyndicationFeed feed = new SyndicationFeed();
XmlQualifiedName key = new XmlQualifiedName("os", "http://www.w3.org/2000/xmlns/");
feed.AttributeExtensions.Add(key, "http://a9.com/-/spec/opensearch/1.1/");

更新:

更仔细地阅读你的问题,我相信你可以通过覆盖的和 WriteStartAttribute所使用的方法的XmlWriter 实例href="http://msdn.microsoft.com/en-us/library/system.servicemodel.syndication.atom10feedformatter.aspx"相对=nofollow> Atom10FeedFormatter 。您可以通过实现如下面的例子定制的XmlWriter类做到这一点。

After reading your question more carefully, I believe you could accomplish this by overriding the WriteStartElement and WriteStartAttribute methods of the XmlWriter instance used by the Atom10FeedFormatter. You can do this by implementing a custom XmlWriter class like the example below.

class AtomXmlTextWriter : XmlTextWriter
{
    private const string Atom10XmlNs = "http://www.w3.org/2005/Atom";
    private const string Atom10XmlNsPrefix = "a";

    public AtomXmlTextWriter(String filename, Encoding encoding)
        : base(filename, encoding)
    {
    }

    public override void WriteStartElement(string prefix, string localName, string ns)
    {
        base.WriteStartElement(GetAtomPrefix(ns), localName, ns);
    }

    public override void WriteStartAttribute(string prefix, string localName, string ns)
    {
        base.WriteStartAttribute(GetAtomPrefix(ns), localName, ns);
    }

    internal string GetAtomPrefix(string ns)
    {
        string prefix = string.Empty;

        if ((ns != null) && (ns.Equals(Atom10XmlNs)))
            prefix = Atom10XmlNsPrefix;

        return prefix;
    }
}

使用自定义类的 Atom10FeedFormatter

SyndicationFeed feed = new SyndicationFeed();
feed.AttributeExtensions.Add(new XmlQualifiedName("os", "http://www.w3.org/2000/xmlns/"),
                             "http://a9.com/-/spec/opensearch/1.1/");

feed.AttributeExtensions.Add(new XmlQualifiedName(null, "http://www.w3.org/2000/xmlns/"),
                             http://schemas.zune.net/catalog/apps/2008/02");

using (XmlWriter writer = new AtomXmlTextWriter(@"TestFeed.xml", Encoding.UTF8))
{
    Atom10FeedFormatter feedFormatter = new Atom10FeedFormatter(feed);
    feedFormatter.WriteTo(writer);
}

产生所需的输出

produces the desired output

<a:feed xmlns:os="http://a9.com/-/spec/opensearch/1.1/"
        xmlns="http://schemas.zune.net/catalog/apps/2008/02"
        xmlns:a="http://www.w3.org/2005/Atom">
    <a:title type="text" />
    <a:id>uuid:0f1b2c84-c935-459e-bc89-79d06b5a976b;id=1</a:id>
    <a:updated>2011-05-21T17:07:46Z</a:updated>
</a:feed>

这篇关于如何创建一个自定义命名空间SyndicationFeed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 22:08