我目前正在使用Google Picasa API尝试列出相册。 OAuth 2.0的代码已编写并正常运行。我可以毫无问题地获取专辑的XML列表。我过去对XML的工作很少,我不能让它反序列化。
我尝试使用XSD.exe工具生成.xsd,然后生成类,但我立即收到错误:名为“id”的列已经属于此DataTable。
我无法弄清楚为什么因为第二个ID标签嵌套在另一个节点中。谁能让我朝着正确的方向前进?
XML:
<?xml version='1.0' encoding='utf-8'?>
<feed xmlns='http://www.w3.org/2005/Atom'
xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/'
xmlns:exif='http://schemas.google.com/photos/exif/2007'
xmlns:geo='http://www.w3.org/2003/01/geo/wgs84_pos#'
xmlns:gml='http://www.opengis.net/gml'
xmlns:georss='http://www.georss.org/georss'
xmlns:batch='http://schemas.google.com/gdata/batch'
xmlns:media='http://search.yahoo.com/mrss/'
xmlns:gphoto='http://schemas.google.com/photos/2007'
xmlns:gd='http://schemas.google.com/g/2005'
gd:etag='W/"CkABRXY8fip7ImA9WxVVGE8."'>
<id>https://picasaweb.google.com/data/feed/api/user/liz</id>
<updated>2009-03-12T01:19:14.876Z</updated>
<category scheme='http://schemas.google.com/g/2005#kind'
term='http://schemas.google.com/photos/2007#user' />
<title>liz</title>
<subtitle></subtitle>
<icon>https://iconPath/liz.jpg</icon>
<link rel='http://schemas.google.com/g/2005#feed'
type='application/atom+xml'
href='https://picasaweb.google.com/data/feed/api/user/liz' />
<link rel='http://schemas.google.com/g/2005#post'
type='application/atom+xml'
href='https://picasaweb.google.com/data/feed/api/user/liz' />
<link rel='alternate' type='text/html'
href='http://picasaweb.google.com/liz' />
<link rel='http://schemas.google.com/photos/2007#slideshow'
type='application/x-shockwave-flash'
href='http://picasaweb.google.com/s/c/bin/slideshow.swf?host=picasaweb.google.com&RGB=0x000000&feed=https%3A%2F%2Fpicasaweb.google.com%2Fdata%2Ffeed%2Fapi%2Fuser%2Fliz%3Falt%3Drss' />
<link rel='self' type='application/atom+xml'
href='https://picasaweb.google.com/data/feed/api/user/liz?start-index=1&max-results=1000&v=2' />
<author>
<name>Liz</name>
<uri>http://picasaweb.google.com/liz</uri>
</author>
<generator version='1.00' uri='http://picasaweb.google.com/'>
Picasaweb</generator>
<openSearch:totalResults>1</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>1000</openSearch:itemsPerPage>
<gphoto:user>liz</gphoto:user>
<gphoto:nickname>Liz</gphoto:nickname>
<gphoto:thumbnail>
https://thumbnailPath/liz.jpg</gphoto:thumbnail>
<gphoto:quotalimit>1073741824</gphoto:quotalimit>
<gphoto:quotacurrent>32716</gphoto:quotacurrent>
<gphoto:maxPhotosPerAlbum>500</gphoto:maxPhotosPerAlbum>
<entry gd:etag='"RXY8fjVSLyp7ImA9WxVVGE8KQAE."'>
<id>https://picasaweb.google.com/data/entry/api/user/liz/albumid/albumID</id>
<published>2005-06-17T07:09:42.000Z</published>
<updated>2009-03-12T01:19:14.000Z</updated>
<app:edited xmlns:app='http://www.w3.org/2007/app'>
2009-03-12T01:19:14.000Z</app:edited>
<category scheme='http://schemas.google.com/g/2005#kind'
term='http://schemas.google.com/photos/2007#album' />
<title>lolcats</title>
<summary>Hilarious Felines</summary>
<rights>public</rights>
<link rel='http://schemas.google.com/g/2005#feed'
type='application/atom+xml'
href='https://picasaweb.google.com/data/feed/api/user/liz/albumid/albumID?authkey=authKey&v=2' />
<link rel='alternate' type='text/html'
href='http://picasaweb.google.com/lh/album/aFDUU2eJpMHZ1dP5TGaYHxtMTjNZETYmyPJy0liipFm0?authkey=authKey' />
<link rel='self' type='application/atom+xml'
href='https://picasaweb.google.com/data/entry/api/user/liz/albumid/albumID?authkey=authKey&v=2' />
<link rel='edit' type='application/atom+xml'
href='https://picasaweb.google.com/data/entry/api/user/liz/albumid/albumID/1236820754876000?authkey=authKey&v=2' />
<link rel='http://schemas.google.com/acl/2007#accessControlList'
type='application/atom+xml'
href='https://picasaweb.google.com/data/entry/api/user/liz/albumid/albumID/acl?authkey=authKey&v=2' />
<author>
<name>Liz</name>
<uri>http://picasaweb.google.com/liz</uri>
</author>
<gphoto:id>albumID</gphoto:id>
<gphoto:location>Mountain View, CA</gphoto:location>
<gphoto:access>public</gphoto:access>
<gphoto:timestamp>1118992182000</gphoto:timestamp>
<gphoto:numphotos>1</gphoto:numphotos>
<gphoto:numphotosremaining>499</gphoto:numphotosremaining>
<gphoto:bytesUsed>23044</gphoto:bytesUsed>
<gphoto:user>liz</gphoto:user>
<gphoto:nickname>Liz</gphoto:nickname>
<media:group>
<media:title type='plain'>lolcats</media:title>
<media:description type='plain'>Hilarious
Felines</media:description>
<media:keywords></media:keywords>
<media:content url='https://imagePath/Lolcats.jpg' type='image/jpeg' medium='image' />
<media:thumbnail url='https://thumbnailPath/Lolcats.jpg' height='160' width='160' />
<media:credit>Liz</media:credit>
</media:group>
</entry>
...more entries similar to the one above here...
</feed>
答案 0 :(得分:2)
我不知道为什么xsd.exe不能正常工作。根据我的经验,为您提供一个良好的开端是很好的,但是为您要序列化的XML文档生成优化的C#类并非万无一失。还有其他类似的代码生成工具。 XsdObjectGen是一个免费的,不确定它是否还在。
在我的评论中,我说另一方面,手动编写你的类以使用Xml Serializer是非常机械的。
这个部分示例应该让你开始:
[XmlType("generator", Namespace="http://www.w3.org/2005/Atom")]
public partial class FeedGenerator
{
[XmlAttribute]
public string version { get; set; }
[XmlAttribute]
public string uri { get; set; }
[XmlText]
public string text { get; set; }
}
[XmlType("category", Namespace="http://www.w3.org/2005/Atom")]
public partial class FeedCategory
{
[XmlAttribute]
public string scheme { get; set; }
[XmlAttribute]
public string term { get; set; }
}
[XmlType("entry", Namespace="http://www.w3.org/2005/Atom")]
public partial class FeedEntry
{
public FeedCategory category { get; set; }
public string title { get; set; }
public string summary { get; set; }
}
[XmlType("author", Namespace="http://www.w3.org/2005/Atom")]
public partial class FeedAuthor
{
public string name { get; set; }
public string uri { get; set; }
}
[XmlType("link", Namespace="http://www.w3.org/2005/Atom")]
public partial class FeedLink
{
[XmlAttribute]
public string rel { get; set; }
[XmlAttribute]
public string @type { get; set; }
[XmlAttribute]
public string href { get; set; }
}
[XmlType("feed", Namespace="http://www.w3.org/2005/Atom")]
[XmlRoot("feed", Namespace="http://www.w3.org/2005/Atom")]
public partial class FeedList
{
public string id { get;set; }
public string title { get;set; }
public string icon { get;set; }
[XmlElement("link")]
public FeedLink[] links { get;set; }
public FeedGenerator generator { get;set; }
[XmlElement(Namespace="http://a9.com/-/spec/opensearch/1.1/")]
public int totalResults { get;set; }
[XmlElement("entry")]
public FeedEntry[] entries { get; set; }
}
此代码仅反序列化FeedList类中的那些元素。
如果需要其他元素,请将它们添加进去。文本元素可以反序列化为简单类型的属性,如字符串,int或DateTime。复杂元素必须是自定义类的属性,使用XmlType
进行修饰。
要反序列化多个元素的序列,例如link
元素或entry
元素,请定义一个数组,并使用XmlElement
对其进行修饰,如图所示。双引号内的值是用于每个引号的元素名称。 XML中的属性使用C#代码中的XmlAttribute
属性修饰属性,引号中的值是attr名称。没有值意味着将xml中与属性名称同名的属性映射到给定属性中。 (用XmlAttribute
修饰的C#代码中名为“rel”的属性被映射到XML文档中名为“rel”的xml属性值。
如果您关心其他模式中的元素(如opensearch模式等),则需要指定与这些模式中的元素对应的属性。同样,在XML中使用复杂类型的自定义类; XML中简单(仅文本)元素的简单C#数据类型。对于简单类型,属性上的XmlElement
属性,或者对于复杂类型,自定义类上的XmlType
属性,必须为该元素指定xmlns名称空间。您无需担心元素前缀 - 命名空间是重要的。请参阅totalResults属性作为示例。
如果XmlElement
或XmlAttribute
属性缺少未标记的字符串值,则假定元素名称或属性名称是属性本身的名称。所以在上面,属性totalResults
反序列化了openSearch:totalResults
元素,因为我提供了与openSearch相对应的xmlns。
要反序列化xml元素的文本节点,请使用XmlText
属性。
扩展上面的示例,您应该能够反序列化反序列化所需的所有内容。
修改强>
但您可能想使用已经构建的库来执行此操作
http://code.google.com/p/google-gdata/updates/list