如何将另一个名称空间添加到WCF SyndicationFeed? [英] How do you add another namespace to WCF SyndicationFeed?

查看:86
本文介绍了如何将另一个名称空间添加到WCF SyndicationFeed?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在供稿生成代码中,我有类似以下内容:

In my feed generation code I have things like:

XNamespace itunesNS = "http://www.itunes.com/dtds/podcast-1.0.dtd";
feed.ElementExtensions.Add(
    new XElement(itunesNS + "subtitle", 
        new XAttribute(XNamespace.Xmlns + "itunes", itunesNS.NamespaceName),
        "sample subtitle").CreateReader());

生成如下内容:

<itunes:subtitle xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">sample subtitle</itunes:subtitle>

如何在channel元素中获取itunes名称空间的声明(xmlns:itunes ="http://www.itunes.com/dtds/podcast-1.0.dtd"),因此不必在每个itunes元素上重复吗?

How can I get the declaration of the itunes namespace (xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd") in the channel element so it doesn't have to be repeated on every itunes element?

我的feed是使用System.ServiceModel.Syndication.SyndicationFeed创建的.

My feed is created with System.ServiceModel.Syndication.SyndicationFeed.

推荐答案

我正确支持iTunes的最终解决方案是:

My end solution for correctly supporting iTunes was:

public class ItunesFeed : SyndicationFeed {
    private string @namespace = "http://www.itunes.com/dtds/podcast-1.0.dtd";
    private string prefix = "itunes";

    public string Subtitle {get; set;}
    public string Author { get; set; }
    public string Summary { get; set; }
    public string OwnerName { get; set; }
    public string OwnerEmail { get; set; }
    public bool Explicit { get; set; }
    public List<List<string>> ItunesCategories = new List<List<string>>();

    protected override void WriteAttributeExtensions(XmlWriter writer, string version) {
        writer.WriteAttributeString("xmlns", prefix, null, @namespace);
    }

    protected override void WriteElementExtensions(XmlWriter writer, string version) {
        WriteItunesElement(writer, "subtitle", Subtitle);
        WriteItunesElement(writer, "author", Author);
        WriteItunesElement(writer, "summary", Summary);
        if (ImageUrl != null) {
            WriteItunesElement(writer, "image", ImageUrl.ToString());
        }
        WriteItunesElement(writer, "explicit", Explicit ? "yes" : "no");

        writer.WriteStartElement(prefix, "owner", @namespace);
        WriteItunesElement(writer, "name", OwnerName);
        WriteItunesElement(writer, "email", OwnerEmail);
        writer.WriteEndElement();

        foreach (var category in ItunesCategories) {
            writer.WriteStartElement(prefix, "category", @namespace);
            writer.WriteAttributeString("text", category[0]);
            if (category.Count == 2) {
                writer.WriteStartElement(prefix, "category", @namespace);
                writer.WriteAttributeString("text", category[1]);
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
        }
    }

    private void WriteItunesElement(XmlWriter writer, string name, string value) {
        if (value != null) {
            writer.WriteStartElement(prefix, name, @namespace);
            writer.WriteValue(value);
            writer.WriteEndElement();
        }
    }
}


public class ItunesItem : SyndicationItem {
    private string @namespace = "http://www.itunes.com/dtds/podcast-1.0.dtd";
    private string prefix = "itunes";

    public string Subtitle { get; set; }
    public string Author { get; set; }
    public string Duration { get; set; }
    public string Keywords { get; set; }
    public bool Explicit { get; set; }

    protected override void WriteElementExtensions(XmlWriter writer, string version) {
        WriteItunesElement(writer, "subtitle", Subtitle);
        WriteItunesElement(writer, "author", Author);
        WriteItunesElement(writer, "summary", Summary.Text);
        WriteItunesElement(writer, "duration", Duration);
        WriteItunesElement(writer, "keywords", Keywords);
        WriteItunesElement(writer, "explicit", Explicit ? "yes" : "no");
    }

    private void WriteItunesElement(XmlWriter writer, string name, string value) {
        if (value != null) {
            writer.WriteStartElement(prefix, name, @namespace);
            writer.WriteValue(value);
            writer.WriteEndElement();
        }
    }
}

这篇关于如何将另一个名称空间添加到WCF SyndicationFeed?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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