1

I asked this question yeasterday but all the answers was not what i was looking for and i think it was much because i didnt fomulate the question that good so i will try again.

I have this 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>

I want to Deserialize this to an .net list object(List<Person> People).

The Person Class:

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




    }


}

Error message: And for those that cant swedish It says after additional information. "There is something wrong in the xml-document (2,2)" And the "response" variable is the xml string

enter image description here

naveen
  • 53,448
  • 46
  • 161
  • 251
Robel Haile
  • 309
  • 1
  • 5
  • 18
  • Have you tried this http://stackoverflow.com/questions/608110/is-it-possible-to-deserialize-xml-into-listt#answer-608181 – Nalin Aggarwal Aug 11 '16 at 18:19
  • 2
    Please tell me that's test data in your XML sample, and that you aren't divulging potentially sensitive information. – Mike Hofer Aug 11 '16 at 18:48
  • How did you generate the serialisation classes? Try running 'xsd.exe people.xml' to generate a schema and then 'xsd.exe people.xsd' to generate the serialisation class. Then your serialisation code should work – nimeshjm Aug 11 '16 at 19:50
  • how do i run 'xsd.exe people.xml' and 'xsd.exe people.xsd' ? @nimeshjm – Robel Haile Aug 11 '16 at 20:05
  • >xsd.exe Command "xsd.exe" is not valid. > this is what i get – Robel Haile Aug 11 '16 at 20:10
  • You start a 'Developer Command Prompt for VS2015'. The shortcut should be in you Start menu or in 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2015\Visual Studio Tools'. – nimeshjm Aug 11 '16 at 20:20
  • xsd.exe C:\Users\robel\Source\Workspaces\Laiosintra2016\ProjectSucces\ProjectSucces\Cont‌​ent\Static\people.xml ...... i write this. and the command promp replys "Access path to C:\Program Files (x86)\Microsoft Visual Studio 14.0\people.xsd have been denied" @nimeshjm – Robel Haile Aug 11 '16 at 20:44
  • It's trying to write to the VS folder. cd to your project folder first. – nimeshjm Aug 11 '16 at 20:48
  • cd to my project folder first? How ? @nimeshjm – Robel Haile Aug 11 '16 at 20:56
  • The code you pasted isn't even valid ... – andrei.ciprian Aug 12 '16 at 09:57
  • @andrei.ciprian howcome? – Robel Haile Aug 12 '16 at 13:29
  • 'cd C:\Users\robel\Source\Workspaces\Laiosintra2016\ProjectSucces\ProjectSucces\Cont‌​‌​ent\Static\' – nimeshjm Aug 12 '16 at 19:47
  • okej i have done it now. i have also run .xsd.exe people.xml' succesfully. but when i run 'xsd.exe people.xsd' i get this error 'Error: Can only generate one of classes or datasets.' what should i do now? @nimeshjm – Robel Haile Aug 13 '16 at 13:57
  • You now have to run xsd.exe /c people.xsd. This will generate a .cs file that you can use to deserialise your xml. – nimeshjm Aug 14 '16 at 20:56
  • it still gets the same error it didnt change a thing. @nimeshjm – Robel Haile Aug 15 '16 at 19:09
  • what should i do with this people.cs class. The way i understand it is that the people.cs class is the model. With that i mean that is the object that the xml will turn into. am i right? @nimeshjm – Robel Haile Aug 16 '16 at 17:20
  • and @andrei.ciprian cAN YOU EXPLAIN WHAT IS WRONG WITH THE CODE? – Robel Haile Aug 16 '16 at 17:22
  • Have you replaced your existing code with person.cs? Also, try changing typeof(List) to typeof(people) – nimeshjm Aug 16 '16 at 18:44
  • yes i have.... check this link. This is how my code looks now http://stackoverflow.com/questions/38982717/weird-error-when-i-try-to-deserialize-a-xml-string-to-a-net-list-object @nimeshjm – Robel Haile Aug 16 '16 at 19:11
  • You've got your answer there :) typeof(people) is the correct type. – nimeshjm Aug 16 '16 at 20:18
  • if you copy/paste the person snippet to rextester or dotnetfiddle it does not even compile – andrei.ciprian Aug 17 '16 at 08:39

0 Answers0