0

这是我的代码:

var objectText = XmlReader.Create(requestedURL);
XmlSerializer mySerializer = new XmlSerializer(typeof(InstagramItems));
var instagramItems = (InstagramItems)mySerializer.Deserialize(objectText);

但似乎它不能与 RSS 一起使用(它们是“或多或少的 XML”):

Server Error - <rss xmlns=''> was not expected.

我该怎么做?我相信有.NET 库而不使用第三部分插件。

RSS 的一部分:

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
<channel>
  <title>Photos tagged as "example" on Instagram</title>
  <link>http://instagram.com</link>
  <description>Photos tagged as "example" on Instagram</description>
  <atom:link href="http://instagram.com/tags/example/feed/recent.rss" rel="self" />
</channel>
4

2 回答 2

2

Step 1: Download the RSS XSD : http://www.thearchitect.co.uk/schemas/rss-2_0.xsd

Step 2: Use xsd.exe to generate an RSS type based on the schema

Step 3: If objectText is only an RSS document, then just substitute your newly created RSS type for InstagramITems in the above code.

Step 3a: If the object text is mixed Instagram and RSS code, then use the DataContractSerializer and pre-register both Instagram and RSS types with the DataContractSerializer before attempting to deserialize

于 2013-07-17T09:52:58.550 回答
1

XmlSerializer works with specific xml format, if you suply any other format other then which it expects the metioned exception will be throwned.

You can parse the xml manualy and create InstagramItems from the parsed xml, I would recommed using linq to xml here is an example http://social.msdn.microsoft.com/Forums/vstudio/en-US/e38e69ac-d325-4cc4-bdf7-bc940e19e63f/read-xml-and-create-objects-using-linq

于 2013-07-17T09:51:40.463 回答