简体   繁体   中英

How to get value from element attribute, XML Serialization

I want to get an attribute from vimeo xml.. here is structure of xml document

<?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> 

and I want to get username attribute from owner element. Here is serialization code.

  [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; }
        }        
}

problem is here i think

      [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">

how can I get attribute from owner..

here is exception detail

I get an exception that is There is an error in XML document (1, 1005).

{"Unexpected node type Element. ReadElementString method can only be called on elements with simple or empty content. Line 1, position 1005."}

System.InvalidOperationException was caught

You need an 'owner' class. You also might consider adding the 'portrait' class with a collection in the 'owner' class.

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; }       

}

You are annotating username with an XmlElementAttribute for "owner." That implies that the owner element should be deserialized into the string property username.

If you want to get at username, you first have to deserialize owner into some object.

For example, you could add an Owner class, in the same manner as you created VimeoSearchResponse.

public class Owner
{
    private string _owner;

    [XmlAttribute]
    public string username 
    {
       get { return _owner; } 
       set { _owner = value; }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM