【问题标题】:How do you add another namespace to WCF SyndicationFeed?如何向 WCF SyndicationFeed 添加另一个命名空间?
【发布时间】:2009-09-03 18:26:47
【问题描述】:

在我的提要生成代码中,我有如下内容:

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>

如何在频道元素中获得 itunes 命名空间 (xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd") 的声明,所以它不必是在每个 iTunes 元素上重复?

我的提要是使用 System.ServiceModel.Syndication.SyndicationFeed 创建的。

【问题讨论】:

    标签: wcf namespaces feed


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      这可能会有所帮助。我正在尝试做同样的事情,但还没有看到全貌......

      http://hyperthink.net/blog/declaring-xml-namespaces-on-a-syndicationfeed/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多