将XML文档反序列化为.NET列表对象

时间:2016-08-11 18:11:41

标签: c# .net xml serialization deserialization

我昨天问了这个问题,但所有答案都不是我想要的,我认为这很多因为我没有提出好的问题,所以我会再试一次。

我有这个xml doxument:

<?xml version="1.0" encoding="UTF-8"?>
<people type="array">
  <person>
    <author-id type="integer">5801</author-id>
    <background></background>
    <company-id type="integer">81881</company-id>
    <created-at type="datetime">2011-08-10T08:39:45Z</created-at>
    <first-name>Pr</first-name>
    <group-id type="integer" nil="true"></group-id>
    <id type="integer">8100134</id>
    <last-name>&quot;Cromwell&quot; (Eriksson)</last-name>
    <owner-id type="integer" nil="true"></owner-id>
    <title>&#228;gare, grafiker</title>
    <updated-at type="datetime">2011-08-16T08:17:43Z</updated-at>
    <visible-to>Everyone</visible-to>
    <company-name>Studio Total</company-name>
    <linkedin-url nil="true"></linkedin-url>
    <avatar_url></avatar_url>
    <contact-data>
      <instant-messengers type="array" />
      <phone-numbers type="array">
        <phone-number>
          <id type="integer">70306123</id>
          <location>Work</location>
          <number>0703689909</number>
        </phone-number>
      </phone-numbers>
      <email-addresses type="array">
        <email-address>
          <address>per@studiototal.se</address>
          <id type="integer">39720318</id>
          <location>Work</location>
        </email-address>
      </email-addresses>
      <twitter-accounts type="array" />
      <addresses type="array" />
      <web-addresses type="array">
        <web-address>
          <id type="integer">70124</id>
          <location>Work</location>
          <url></url>
        </web-address>
      </web-addresses>
    </contact-data>
  </person>
  <person>
    <author-id type="integer">257</author-id>
    <background></background>
    <company-id type="integer">158696</company-id>
    <created-at type="datetime">2013-02-18T12:49:37Z</created-at>
    <first-name></first-name>
    <group-id type="integer" nil="true"></group-id>
    <id type="integer">1538730</id>
    <last-name>&quot;Malmer&quot;</last-name>
    <owner-id type="integer" nil="true"></owner-id>
    <title>Projektledare Online listening</title>
    <updated-at type="datetime">2013-02-18T12:49:37Z</updated-at>
    <visible-to>Everyone</visible-to>
    <company-name>Santa Maria</company-name>
    <linkedin-url nil="true"></linkedin-url>
    <avatar_url></avatar_url>
    <contact-data>
      <instant-messengers type="array" />
      <phone-numbers type="array">
        <phone-number>
          <id type="integer">16649</id>
          <location>Work</location>
          <number>03-151</number>
        </phone-number>
      </phone-numbers>
      <email-addresses type="array" />
      <twitter-accounts type="array" />
      <addresses type="array" />
      <web-addresses type="array" />
    </contact-data>
  </person>
  <person>
    <author-id type="integer">8257</author-id>
    <background></background>
    <company-id type="integer">1518665</company-id>
    <created-at type="datetime">2013-02-01T10:14:27Z</created-at>
    <first-name>&quot;Sorush&quot;</first-name>
    <group-id type="integer" nil="true"></group-id>
    <id type="integer">151827</id>
    <last-name></last-name>
    <owner-id type="integer" nil="true"></owner-id>
    <title></title>
    <updated-at type="datetime">2013-02-01T10:16:29Z</updated-at>
    <visible-to>Everyone</visible-to>
    <company-name>Rancold</company-name>
    <linkedin-url nil="true"></linkedin-url>
    <avatar_url></avatar_url>
    <contact-data>
      <instant-messengers type="array" />
      <phone-numbers type="array">
        <phone-number>
          <id type="integer">127275</id>
          <location>Work</location>
          <number></number>
        </phone-number>
      </phone-numbers>
      <email-addresses type="array">
        <email-address>
          <address>sjkka@rakkncold.com</address>
          <id type="integer">76736018</id>
          <location>Work</location>
        </email-address>
      </email-addresses>
      <twitter-accounts type="array" />
      <addresses type="array" />
      <web-addresses type="array">
        <web-address>
          <id type="integer">127976</id>
          <location>Work</location>
          <url>http://www.rancoltyd.com</url>
        </web-address>
      </web-addresses>
    </contact-data>
  </person>
  <!--there are many more <person> elements-->
</people>

我想将其反序列化为.net列表对象(List&lt; Person&gt; People)。

人员类:

public class Person
{

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class people
    {

        private peoplePerson[] personField;

        private string typeField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("person")]
        public peoplePerson[] person
        {
            get
            {
                return this.personField;
            }
            set
            {
                this.personField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string type
        {
            get
            {
                return this.typeField;
            }
            set
            {
                this.typeField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class peoplePerson
    {

        private peoplePersonAuthorid authoridField;

        private string backgroundField;

        private peoplePersonCompanyid companyidField;

        private peoplePersonCreatedat createdatField;

        private string firstnameField;

        private peoplePersonGroupid groupidField;

        private peoplePersonID idField;

        private string lastnameField;

        private peoplePersonOwnerid owneridField;

        private string titleField;

        private peoplePersonUpdatedat updatedatField;

        private string visibletoField;

        private string companynameField;

        private peoplePersonLinkedinurl linkedinurlField;

        private string avatar_urlField;

        private peoplePersonContactdata contactdataField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("author-id")]
        public peoplePersonAuthorid authorid
        {
            get
            {
                return this.authoridField;
            }
            set
            {
                this.authoridField = value;
            }
        }

        /// <remarks/>
        public string background
        {
            get
            {
                return this.backgroundField;
            }
            set
            {
                this.backgroundField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("company-id")]
        public peoplePersonCompanyid companyid
        {
            get
            {
                return this.companyidField;
            }
            set
            {
                this.companyidField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("created-at")]
        public peoplePersonCreatedat createdat
        {
            get
            {
                return this.createdatField;
            }
            set
            {
                this.createdatField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("first-name")]
        public string firstname
        {
            get
            {
                return this.firstnameField;
            }
            set
            {
                this.firstnameField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("group-id")]
        public peoplePersonGroupid groupid
        {
            get
            {
                return this.groupidField;
            }
            set
            {
                this.groupidField = value;
            }
        }

        /// <remarks/>
        public peoplePersonID id
        {
            get
            {
                return this.idField;
            }
            set
            {
                this.idField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("last-name")]
        public string lastname
        {
            get
            {
                return this.lastnameField;
            }
            set
            {
                this.lastnameField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("owner-id")]
        public peoplePersonOwnerid ownerid
        {
            get
            {
                return this.owneridField;
            }
            set
            {
                this.owneridField = value;
            }
        }

        /// <remarks/>
        public string title
        {
            get
            {
                return this.titleField;
            }
            set
            {
                this.titleField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("updated-at")]
        public peoplePersonUpdatedat updatedat
        {
            get
            {
                return this.updatedatField;
            }
            set
            {
                this.updatedatField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("visible-to")]
        public string visibleto
        {
            get
            {
                return this.visibletoField;
            }
            set
            {
                this.visibletoField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("company-name")]
        public string companyname
        {
            get
            {
                return this.companynameField;
            }
            set
            {
                this.companynameField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("linkedin-url")]
        public peoplePersonLinkedinurl linkedinurl
        {
            get
            {
                return this.linkedinurlField;
            }
            set
            {
                this.linkedinurlField = value;
            }
        }

        /// <remarks/>
        public string avatar_url
        {
            get
            {
                return this.avatar_urlField;
            }
            set
            {
                this.avatar_urlField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("contact-data")]
        public peoplePersonContactdata contactdata
        {
            get
            {
                return this.contactdataField;
            }
            set
            {
                this.contactdataField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class peoplePersonAuthorid
    {

        private string typeField;

        private uint valueField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string type
        {
            get
            {
                return this.typeField;
            }
            set
            {
                this.typeField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTextAttribute()]
        public uint Value
        {
            get
            {
                return this.valueField;
            }
            set
            {
                this.valueField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class peoplePersonCompanyid
    {

        private string typeField;

        private uint valueField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string type
        {
            get
            {
                return this.typeField;
            }
            set
            {
                this.typeField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTextAttribute()]
        public uint Value
        {
            get
            {
                return this.valueField;
            }
            set
            {
                this.valueField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class peoplePersonCreatedat
    {

        private string typeField;

        private System.DateTime valueField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string type
        {
            get
            {
                return this.typeField;
            }
            set
            {
                this.typeField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTextAttribute()]
        public System.DateTime Value
        {
            get
            {
                return this.valueField;
            }
            set
            {
                this.valueField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class peoplePersonGroupid
    {

        private string typeField;

        private bool nilField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string type
        {
            get
            {
                return this.typeField;
            }
            set
            {
                this.typeField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public bool nil
        {
            get
            {
                return this.nilField;
            }
            set
            {
                this.nilField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class peoplePersonID
    {

        private string typeField;

        private uint valueField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string type
        {
            get
            {
                return this.typeField;
            }
            set
            {
                this.typeField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTextAttribute()]
        public uint Value
        {
            get
            {
                return this.valueField;
            }
            set
            {
                this.valueField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class peoplePersonOwnerid
    {

        private string typeField;

        private bool nilField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string type
        {
            get
            {
                return this.typeField;
            }
            set
            {
                this.typeField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public bool nil
        {
            get
            {
                return this.nilField;
            }
            set
            {
                this.nilField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class peoplePersonUpdatedat
    {

        private string typeField;

        private System.DateTime valueField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string type
        {
            get
            {
                return this.typeField;
            }
            set
            {
                this.typeField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTextAttribute()]
        public System.DateTime Value
        {
            get
            {
                return this.valueField;
            }
            set
            {
                this.valueField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class peoplePersonLinkedinurl
    {

        private bool nilField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public bool nil
        {
            get
            {
                return this.nilField;
            }
            set
            {
                this.nilField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class peoplePersonContactdata
    {

        private peoplePersonContactdataInstantmessengers instantmessengersField;

        private peoplePersonContactdataPhonenumbers phonenumbersField;

        private peoplePersonContactdataEmailaddresses emailaddressesField;

        private peoplePersonContactdataTwitteraccounts twitteraccountsField;

        private peoplePersonContactdataAddresses addressesField;

        private peoplePersonContactdataWebaddresses webaddressesField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("instant-messengers")]
        public peoplePersonContactdataInstantmessengers instantmessengers
        {
            get
            {
                return this.instantmessengersField;
            }
            set
            {
                this.instantmessengersField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("phone-numbers")]
        public peoplePersonContactdataPhonenumbers phonenumbers
        {
            get
            {
                return this.phonenumbersField;
            }
            set
            {
                this.phonenumbersField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("email-addresses")]
        public peoplePersonContactdataEmailaddresses emailaddresses
        {
            get
            {
                return this.emailaddressesField;
            }
            set
            {
                this.emailaddressesField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("twitter-accounts")]
        public peoplePersonContactdataTwitteraccounts twitteraccounts
        {
            get
            {
                return this.twitteraccountsField;
            }
            set
            {
                this.twitteraccountsField = value;
            }
        }

        /// <remarks/>
        public peoplePersonContactdataAddresses addresses
        {
            get
            {
                return this.addressesField;
            }
            set
            {
                this.addressesField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("web-addresses")]
        public peoplePersonContactdataWebaddresses webaddresses
        {
            get
            {
                return this.webaddressesField;
            }
            set
            {
                this.webaddressesField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class peoplePersonContactdataInstantmessengers
    {

        private string typeField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string type
        {
            get
            {
                return this.typeField;
            }
            set
            {
                this.typeField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class peoplePersonContactdataPhonenumbers
    {

        private peoplePersonContactdataPhonenumbersPhonenumber phonenumberField;

        private string typeField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("phone-number")]
        public peoplePersonContactdataPhonenumbersPhonenumber phonenumber
        {
            get
            {
                return this.phonenumberField;
            }
            set
            {
                this.phonenumberField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string type
        {
            get
            {
                return this.typeField;
            }
            set
            {
                this.typeField = value;
            }
        }
    }




    }


}

错误讯息: 对于那些不能瑞典语的人来说,在获得更多信息之后会说。 “xml-document(2,2)中出现了问题” 而“response”变量是xml字符串

enter image description here

0 个答案:

没有答案