使用ASP.Net Web API在ATOM中缺少“feed”元素的xmlns命名空间属性

时间:2013-04-11 16:34:07

标签: asp.net-web-api atom-feed

我正在开发一个ASP.Net Web API项目。

需要在应用程序中更改资源时返回ATOM提要。

我将Feed全部工作但由于某种原因,我的feed元素省略了'xmlns'元素,该元素将XML描述为ATOM源。

期望的结果是:

<?xml version="1.0" encoding="utf-8" ?>
  <feed xmlns="http://www.w3.org/2005/Atom">
    <title type="text">My Feed</title>
    <id>uuid:ff389357-aaab-4814-8916-b8fff325e6bb;id=22</id>
    <updated>2013-04-11T16:15:10Z</updated>
    <entry>
      <id>uuid:ff389357-aaab-4814-8916-b8fff325e6bb;id=23</id>
      <title type="text">Data.Parliament Resource 1</title>
      <updated>2013-04-11T16:15:10Z</updated>
      <author>
        <name>Parliament System X</name>
      </author>
     </entry>
  </feed>

但我的Feed看起来像:

<?xml version="1.0" encoding="utf-8" ?>
  <feed>
    <title type="text">My Feed</title>
    <id>uuid:ff389357-aaab-4814-8916-b8fff325e6bb;id=22</id>
    <updated>2013-04-11T16:15:10Z</updated>
    <entry>
      <id>uuid:ff389357-aaab-4814-8916-b8fff325e6bb;id=23</id>
      <title type="text">Data.Parliament Resource 1</title>
      <updated>2013-04-11T16:15:10Z</updated>
      <author>
        <name>Parliament System X</name>
      </author>
     </entry>
  </feed>

注意'feed'元素上缺少'xmlns'属性。

我搜索得很高,但没有用到......

问题:

任何人都可以向我解释为什么'xmlns'元素缺失以及如何将其恢复到那里???

以下是ATOM媒体类型格式化程序的代码:

public class AtomMediaTypeFormatter : BufferedMediaTypeFormatter
{
    private const string AtomMediaType = "application/atom+xml";
    private IFeedHelper _feedService;

    public AtomMediaTypeFormatter(IFeedHelper feedService)
    {
        this._feedService = feedService;
        SupportedMediaTypes.Add(new MediaTypeHeaderValue(AtomMediaType));
        this.AddQueryStringMapping("format", "atom", AtomMediaType);
    }

    public override bool CanWriteType(Type type)
    {
        return type.Implements<IFeedEntry>() || type.Implements<IFeed>();
    }

    /// <summary>
    /// DDP is not currently supporting this operation.
    /// </summary>
    public override object ReadFromStream(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
    {
        throw new NotImplementedException();
    }

    /// <summary>
    /// Writes the given object value of type <param name="type"></param> to the given stream
    /// </summary>
    public override void WriteToStream(Type type, object value, Stream writeStream, HttpContent content)
    {
        using (writeStream)
        {
            var feed = value as IFeed;
            if (feed != null)
            {
                WriteAtomFeed(feed, writeStream);
            }
            else
            {
                WriteAtomEntry((IFeedEntry)value, writeStream);
            }
        }
    }

    private void WriteAtomFeed(IFeed feed, Stream writeStream)
    {
        var formatter = new Atom10FeedFormatter(_feedService.Syndicate(feed));

        using (var writer = XmlWriter.Create(writeStream))
        {
            formatter.WriteTo(writer);
        }
    }

    private void WriteAtomEntry(IFeedEntry feedEntry, Stream writeStream)
    {
        var formatter = new Atom10ItemFormatter(_feedService.Syndicate(feedEntry));

        using (var writer = XmlWriter.Create(writeStream))
        {
            formatter.WriteTo(writer);
        }
    }
}

这是FeedService.Sydnicate(IFeed)方法的代码:

    /// <summary>
    /// Instantiates and returns a SyndicationFeed using the values of the given IFeed instance
    /// </summary>
    public SyndicationFeed Syndicate(IFeed feed)
    {
        var atomFeed = new SyndicationFeed
        {
            Title = new TextSyndicationContent(feed.Title),
            Id = feed.Id,
            Items = feed.Items.Select(i => Syndicate(i)),
            LastUpdatedTime = feed.LastUpdated
        };

        atomFeed.Links.Add(new SyndicationLink(new Uri(feed.Link.Href), feed.Link.Href, "", "", default(long)));

        return atomFeed;
    }

最后是FeedService.Sydnicate(IFeedEntry):

    /// <summary>
    /// Instantiates and returns a SyndicationItem using the values of the given IFeedEntry instance
    /// </summary>
    public SyndicationItem Syndicate(IFeedEntry feedEntry)
    {
        var item = new SyndicationItem
        {
            Id = feedEntry.Id,
            Title = new TextSyndicationContent(feedEntry.Title, TextSyndicationContentKind.Plaintext),
            LastUpdatedTime = feedEntry.LastUpdated
        };
        item.Authors.Add(new SyndicationPerson("", feedEntry.Author, ""));
        if (feedEntry.Links != null)
        {
            foreach (var link in feedEntry.Links)
            {
                item.Links.Add(new SyndicationLink(new Uri(link.ToString())));
            }
        }
        return item;
    }

1 个答案:

答案 0 :(得分:0)

我无法帮助您解释Atom10FeedFormatter未添加命名空间的原因,但如果您找不到解决方案,我可能会有其他选择。我玩过ODataMessageWriter来编写Atom内容。您可以看到源代码here 输出(带命名空间)是here