Google Picasa API XML反序列化

Google Picasa API XML反序列化,xml,picasa,Xml,Picasa,我目前正在与谷歌Picasa API合作,试图列出专辑。 OAuth 2.0的代码已编写并运行。我可以毫无问题地获得相册的XML列表。在过去,我很少使用XML,也无法对其进行反序列化 我试图使用XSD.exe工具生成一个.XSD,然后从中生成类,但我立即得到一个错误:名为“id”的列已经属于此数据表 我不明白为什么,因为第二个ID标记嵌套在另一个节点中。有人能给我指出正确的方向吗 XML: https://picasaweb.google.com/data/feed/api/user/liz

我目前正在与谷歌Picasa API合作,试图列出专辑。 OAuth 2.0的代码已编写并运行。我可以毫无问题地获得相册的XML列表。在过去,我很少使用XML,也无法对其进行反序列化

我试图使用XSD.exe工具生成一个.XSD,然后从中生成类,但我立即得到一个错误:名为“id”的列已经属于此数据表

我不明白为什么,因为第二个ID标记嵌套在另一个节点中。有人能给我指出正确的方向吗

XML:


https://picasaweb.google.com/data/feed/api/user/liz
2009-03-12T01:19:14.876Z
利兹
https://iconPath/liz.jpg
利兹
http://picasaweb.google.com/liz
皮卡萨韦布
1.
1.
1000
利兹
利兹
https://thumbnailPath/liz.jpg
1073741824
32716
500
https://picasaweb.google.com/data/entry/api/user/liz/albumid/albumID
2005-06-17T07:09:42.000Z
2009-03-12T01:19:14.000Z
2009-03-12T01:19:14.000Z
棒棒糖
滑稽的猫科动物
公众的
利兹
http://picasaweb.google.com/liz
白蛋白
加利福尼亚州山景城
公众的
1118992182000
1.
499
23044
利兹
利兹
棒棒糖
令人捧腹的
猫科动物
利兹
…更多类似于上面的条目。。。

我不知道xsd.exe为什么不工作。根据我的经验,给您一个良好的开端是很好的,但在为要序列化的XML文档生成优化的C#类时,这并不是万无一失的。还有其他代码生成工具也有类似的功能。XsdObjectGen是免费的,不确定它是否还在

在我的评论中,我说另一方面,手工编写类代码以使用Xml序列化程序是非常机械的

这个局部示例应该让您开始:

[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
修饰

要反序列化多个元素的序列,如
链接
元素或
条目
元素,请定义一个数组,并用
XmlElement
装饰它,如图所示。双引号内的值是每个元素使用的元素名称。XML中的属性在C#code中使用
xmldattribute
属性修饰属性,引号中的值是attr名称。无值表示将xml中与属性名同名的属性映射到给定的属性中。(用
xmldattribute
修饰的C#code中名为“rel”的属性映射到xml文档中名为“rel”的xml属性值)

如果您关心其他模式(如opensearch模式等)中的元素,则需要指定与这些模式中的元素对应的属性。同样,对XML中的复杂类型使用自定义类;XML中简单(纯文本)元素的简单C#数据类型。对于简单类型,属性上的
xmlmelement
属性,或者对于复杂类型,自定义类上的
XmlType
属性,必须为该元素指定xmlns命名空间。您不必担心元素前缀——名称空间才是最重要的。请参见totalResults属性作为示例

xmlement
xmldattribute
属性缺少未标记的字符串值时,将假定元素名或属性名为属性本身的名称。因此在上面,属性
totalResults
反序列化了
openSearch:totalResults
元素,因为我提供了与openSearch相对应的xmlns

要反序列化xml元素的文本节点,请使用
XmlText
属性

扩展上面的示例,您应该能够反序列化需要反序列化的所有内容


编辑
但您可能需要使用已经构建的库来完成此操作。

您在xsd.exe中使用的命令行是什么?您是否使用了
/c
开关?谢谢您的回复!我没走那么远。。。。XSD.exe google.xml-要生成.XSD,请运行XSD.exe google.XSD/c,根据文档生成类。。。。或者我错过了什么?等等,检查一下。我刚刚意识到Google数据是一个Atom提要,所有其他xmlns内容都引用了您需要定义或提供给xsd.exe的其他模式。这行不通。另一方面,手工编写类代码以使用Xml序列化程序是非常机械的。非常感谢!你让我走对了路!我不想使用该库,因为它适用于api 1.0版,我想使用2.0版。再次感谢您朝着正确的方向努力!一切都很顺利!我设法反序列化了大部分内容,但遇到了一个小问题,我想你可以向我解释一下。。。。在条目部分,我添加了以下内容(添加了很多洞,但注释中有很多字符):下一条注释中的代码。。。。除了返回null的numphotosResisting和bytesUsed外,所有操作都正常。[xmlement(ElementName=“numphotos”,Namespace=“public string gnumphotos{get;set;}[xmlement(ElementName=“numphotosResisting”,Namespace=“public string gnumphotosResisting{get;set;}[xmlement(ElementName=“bytesUsed”,Namespace=“public string gbytesUsed{get;set;}[xmlement(ElementName=“user”,Namespace=“public string guser{get;set;}我想你需要问另一个问题,一个新的问题。把完整的代码发布到那里,而不是试图把代码塞进注释中。很难去解释
[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; }
}