How to create a SyndicationFeed with a custom namespace

空扰寡人 提交于 2019-12-05 02:58:01

问题


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/");

Thanks!


回答1:


I believe it should be

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/");

UPDATE:

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;
    }
}

Using your custom class with the 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>


来源:https://stackoverflow.com/questions/6078777/how-to-create-a-syndicationfeed-with-a-custom-namespace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!