Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从元素属性、XML序列化中获取值_C#_Xml Serialization - Fatal编程技术网

C# 如何从元素属性、XML序列化中获取值

C# 如何从元素属性、XML序列化中获取值,c#,xml-serialization,C#,Xml Serialization,我想从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=

我想从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> 
我想问题就在这里

      [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">
[xmlementAttribute(“所有者”)]
公共字符串用户名
{
获取{return\u username;}
设置{u username=value;}
}
- 
如何从所有者处获取属性

这里是例外细节

我得到一个异常,XML文档中有一个错误(1, 1005).

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

捕获到System.InvalidOperationException


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

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

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

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

}

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

如果您想获取用户名,首先必须将所有者反序列化到某个对象中

例如,您可以按照创建VimeoSearchResponse的相同方式添加所有者类

public class Owner
{
    private string _owner;

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

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

如果您想获取用户名,首先必须将所有者反序列化到某个对象中

例如,您可以按照创建VimeoSearchResponse的相同方式添加所有者类

public class Owner
{
    private string _owner;

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

你有例外吗?是否分配了错误的值?@IAbstract,我编辑了我的问题。。我想,它找不到所有者元素..你有例外吗?是否分配了错误的值?@IAbstract,我编辑了我的问题。。我想,它找不到所有者元素..Thanx,我也这么想,但我不知道如何使它成为SearchResponseVideo SwrapperVideo类的属性?@HarunAbi-没问题。。很高兴能帮上忙。Thanx,我也这么想,但我不知道如何使它成为SearchResponseVideo类的属性?@HarunAbi-没问题。。很乐意帮忙。