Reputation: 303
I'm trying to set up an RSS feed using the Syndicate feed object that will return to a WPF application that uses an XMLDataProvider. I keep getting this
Data at the root level is invalid>
Here's my current code for the rss feed. This shows up fine in the browser as XML.
[Route("test")]
public Rss20FeedFormatter Get()
{
var feed = new SyndicationFeed("Test Feed", "This is a test feed",
new Uri("http://google.com"));
feed.Categories.Add(new SyndicationCategory("test"));
feed.Description = new TextSyndicationContent("This is a test feed to see how easy it is");
var test = new SyndicationItem("[email protected]", "this is a note",
new Uri("http://google.com"), "[email protected]",
DateTime.Now);
test.Categories.Add(new SyndicationCategory("Person"));
test.Authors.Add(new SyndicationPerson("[email protected]"));
var items = new List<SyndicationItem> {test};
feed.Items = items;
return new Rss20FeedFormatter(feed, false);
}
and then the XAML code:
<Window x:Class="RSSReader.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<XmlDataProvider Source="http://localhost:8080/api/test/"
x:Key="xdata" XPath="//item"></XmlDataProvider>
</Window.Resources>
<Grid>
<ListBox ItemsSource="{Binding Source={StaticResource xdata}}">
</ListBox>
</Grid>
I've tried this with the Microsoft feeds: http://www.microsoft.com/en-us/news/rss/rssfeed.aspx?ContentType=PressReleases and it works but with mine I get that error
EDIT Here's the xml that is returned from the api
<rss version="2.0">
<channel>
<title>Test Feed</title>
<link>http://google.com/</link>
<description>This is a test feed to see how easy it is</description>
<category>test</category>
<item>
<guid isPermaLink="false">[email protected]</guid>
<link>http://google.com/</link>
<author>[email protected]</author>
<category>Person</category>
<title>[email protected]</title>
<description>this is a note</description>
</item>
</channel>
</rss>
Upvotes: 0
Views: 268
Reputation: 303
So turns out that the Rss20FeedFormatter doesn't put xml tags at the top of the rss stream, so the dataprovider fails to read it.
Instead, going to write the formatter to an xmlwriter and add the tags in that way
EDIT I've added the code below for how this was achieved. It was a bit of a mess around to get the utf encoding correct.
[Route("test")]
public HttpResponseMessage Get()
{
var feed = new SyndicationFeed("Test Feed", "This is a test feed",
new Uri("http://google.com"));
feed.Categories.Add(new SyndicationCategory("test"));
feed.Description = new TextSyndicationContent("This is a test feed to see how easy it is");
var test = new SyndicationItem("[email protected]", "this is a note",
new Uri("http://google.com"), "[email protected]",
DateTime.Now);
test.Categories.Add(new SyndicationCategory("Person"));
test.Authors.Add(new SyndicationPerson("[email protected]"));
var test2 = new SyndicationItem("[email protected]", "this is a note",
new Uri("http://google.com"), "[email protected]",
DateTime.Now);
test2.Categories.Add(new SyndicationCategory("Person"));
test2.Authors.Add(new SyndicationPerson("[email protected]"));
var items = new List<SyndicationItem> { test, test2 };
feed.Items = items;
var formatter = new Rss20FeedFormatter(feed);
var output = new MemoryStream();
var xws = new XmlWriterSettings {Encoding = Encoding.UTF8};
using (var xmlWriter = XmlWriter.Create(output, xws))
{
formatter.WriteTo(xmlWriter);
xmlWriter.Flush();
}
string xml;
using (var sr = new StreamReader(output))
{
output.Position = 0;
xml = sr.ReadToEnd();
sr.Close();
}
var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(xml, Encoding.UTF8, "application/xml")};
return response;
}
Upvotes: 0