0

我想从 vimeo xml 中获取一个属性。这里是 xml 文档的结构

<?xml version="1.0" encoding="UTF-8" ?> 
- <rsp generated_in="0.6533" stat="ok">
- <videos on_this_page="15" page="1" perpage="15" total="329">
- <video allow_adds="1" embed_privacy="anywhere" id="3475223" is_hd="0" is_transcoding="0" license="0" privacy="anybody">
  <title>AxDroid - Android on Dell Axim x51v</title> 
  <description>This is my first attempt at installing and running Android on my Dell Axim x51v. Touchscreen and buttons are working! For details please visit: http://axdroid.blogspot.com/</description> 
  <upload_date>2009-03-04 16:14:19</upload_date> 
  <modified_date>2012-07-14 07:03:32</modified_date> 
  <number_of_likes>2</number_of_likes> 
  <number_of_plays>43422</number_of_plays> 
  <number_of_comments>1</number_of_comments> 
  <width>320</width> 
  <height>240</height> 
  <duration>320</duration> 
- <owner display_name="Ertan D." id="1387509" is_plus="0" is_pro="0" is_staff="0" profileurl="http://vimeo.com/user1387509" realname="Ertan D." username="user1387509" videosurl="http://vimeo.com/user1387509/videos">
- <portraits>
  <portrait height="30" width="30">http://a.vimeocdn.com/images_v6/portraits/portrait_30_yellow.png</portrait> 
  <portrait height="75" width="75">http://a.vimeocdn.com/images_v6/portraits/portrait_75_yellow.png</portrait> 
  <portrait height="100" width="100">http://a.vimeocdn.com/images_v6/portraits/portrait_100_yellow.png</portrait> 
  <portrait height="300" width="300">http://a.vimeocdn.com/images_v6/portraits/portrait_300_yellow.png</portrait> 
  </portraits>
  </owner>
- <tags>
  <tag author="1387509" id="8397224" normalized="android" url="http://vimeo.com/tag:android">android</tag> 
  <tag author="1387509" id="8397225" normalized="dell" url="http://vimeo.com/tag:dell">dell</tag> 
  <tag author="1387509" id="8397226" normalized="axim" url="http://vimeo.com/tag:axim">axim</tag> 
  <tag author="1387509" id="8397227" normalized="linux" url="http://vimeo.com/tag:linux">linux</tag> 
  <tag author="1387509" id="8397228" normalized="google" url="http://vimeo.com/tag:google">google</tag> 
  <tag author="1387509" id="8397229" normalized="pda" url="http://vimeo.com/tag:pda">pda</tag> 
  <tag author="1387509" id="8397230" normalized="ppc" url="http://vimeo.com/tag:ppc">ppc</tag> 
  </tags>
- <cast>
  <member display_name="Ertan D." id="1387509" role="" username="user1387509" /> 
  </cast>
- <urls>
  <url type="video">http://vimeo.com/3475223</url> 
  </urls>
- <thumbnails>
  <thumbnail height="75" width="100">http://b.vimeocdn.com/ts/347/807/3478071_100.jpg</thumbnail> 
  <thumbnail height="150" width="200">http://b.vimeocdn.com/ts/347/807/3478071_200.jpg</thumbnail> 
  <thumbnail height="480" width="640">http://b.vimeocdn.com/ts/347/807/3478071_640.jpg</thumbnail> 
  </thumbnails>
  </video>
- <video allow_adds="1" embed_privacy="anywhere" id="28665952" is_hd="1" is_transcoding="0" license="0" privacy="anybody">
  <title>Duygu + Ertan Şıkır Şıkır by DÜĞÜNHİKAYEMİZ</title> 
  <description /> 
  <upload_date>2011-09-06 10:54:49</upload_date> 
  <modified_date>2012-07-14 06:41:33</modified_date> 
  <number_of_likes>3</number_of_likes> 
  <number_of_plays>26214</number_of_plays> 

我想从所有者元素中获取用户名属性。这是序列化代码。

  [XmlTypeAttribute(AnonymousType = true)]
    [XmlRootAttribute(Namespace = "", IsNullable = false, ElementName = "rsp")]
    public partial class VimeoSearchResponse
    {
        private SearchResponseVideosWrapper _videos;
        private string _generated_in;
        private string _stat;

        [XmlElementAttribute("videos", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public SearchResponseVideosWrapper videos
        {
            get { return _videos; }
            set { _videos = value; }
        }
        [XmlAttributeAttribute()]
        public string generated_in
        {
            get { return _generated_in; }
            set { _generated_in = value; }
        }
        [XmlAttributeAttribute()]
        public string stat
        {
            get { return _stat; }
            set { _stat = value; }
        }
    }

    [SerializableAttribute]
    [XmlTypeAttribute(AnonymousType = true)]
    public partial class SearchResponseVideosWrapper
    {
        private SearchResponseVideosWrapperVideo[] _video;
        private string _on_this_page;
        private string _page;
        private string _perpage;
        private string _total;

        [XmlElementAttribute("video", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public SearchResponseVideosWrapperVideo[] video
        {
            get { return _video; }
            set { _video = value; }
        }


        [XmlAttributeAttribute()]
        public string on_this_page
        {
            get { return _on_this_page; }
            set { _on_this_page = value; }
        }
        [XmlAttributeAttribute()]
        public string page
        {
            get { return _page; }
            set { _page = value; }
        }
        [XmlAttributeAttribute()]
        public string perpage
        {
            get { return _perpage; }
            set { _perpage = value; }
        }
        [XmlAttributeAttribute()]
        public string total
        {
            get { return _total; }
            set { _total = value; }
        }
    }

    [SerializableAttribute]
    [XmlTypeAttribute(AnonymousType = true)]
    public partial class SearchResponseVideosWrapperVideo
    {
        private string _title;
        private string _id;
        private string _username;

        [XmlElement()]
        public string title
        {
            get { return _title; }
            set { _title = value; }
        }

        [XmlAttributeAttribute()]
        public string id
        {
            get { return _id; }
            set { _id = value; }
        }

        [XmlElementAttribute("owner")]
        public string username 
        {
            get { return _username; }
            set { _username = value; }
        }        
}

我认为问题就在这里

      [XmlElementAttribute("owner")]
        public string username 
        {
            get { return _username; }
            set { _username = value; }
        }

- <owner display_name="Ertan D." id="1387509" is_plus="0" is_pro="0" is_staff="0" profileurl="http://vimeo.com/user1387509" realname="Ertan D." username="user1387509" videosurl="http://vimeo.com/user1387509/videos">

我怎样才能从所有者那里获得属性..

这是异常详细信息

我收到一个异常,即 XML 文档中存在错误 (1, 1005)。

{“非预期的节点类型元素。ReadElementString 方法只能对内容简单或为空的元素调用。第 1 行,位置 1005。”}

System.InvalidOperationException 被捕获

4

2 回答 2

2

你需要一个“所有者”类。您还可以考虑在“所有者”类中添加带有集合的“肖像”类。

public class owner
{
    [XmlAttributeAttribute]
    public string username { get; set; }
}



[SerializableAttribute] 
[XmlTypeAttribute(AnonymousType = true)] 
public partial class SearchResponseVideosWrapperVideo 
{ 
    private string _title; 
    private string _id; 
    private string _username; 

    [XmlElement()] 
    public string title 
    { 
        get { return _title; } 
        set { _title = value; } 
    } 

    [XmlAttributeAttribute()] 
    public string id 
    { 
        get { return _id; } 
        set { _id = value; } 
    } 

    [XmlElementAttribute("owner")] 
    public owner owner { get; set; }       

}

于 2012-07-14T13:14:45.600 回答
1

您正在使用“所有者”的 XmlElementAttribute 注释用户名。这意味着所有者元素应反序列化为字符串属性用户名。

如果要获取用户名,首先必须将所有者反序列化为某个对象。

例如,您可以使用与创建 VimeoSearchResponse 相同的方式添加 Owner 类。

public class Owner
{
    private string _owner;

    [XmlAttribute]
    public string username 
    {
       get { return _owner; } 
       set { _owner = value; }
    }
}
于 2012-07-14T13:24:24.147 回答