locked
xml Deserialize error RRS feed

  • Question

  • Hi I am having a problem with the root and namespace for my xml i keep getting this error. {"OpenSearchDescription xmlns='' was not expected." System.Exception {System.InvalidOperationException}. this is the xml i an trying to deserialize. http://api.themoviedb.org/2.1/Person.search/en/xml/b64bb199343edf728721342eac53a677/Brad+Pitt this is my root xml [XmlRoot("OpenSearchDescription", Namespace = "http://a9.com/-/spec/opensearch/1.1/")] public class ShowActorsInfo { [XmlElement("people")] public GetActorsInfo GetActorInfo { get; set; } public ShowActorsInfo() { GetActorInfo = new GetActorsInfo(); } } public class GetActorsInfo { [XmlElement("person")] public List Info { get; set; } } can someone tell me what could be wrong?
    Thursday, October 28, 2010 6:48 PM

All replies

  • CAN ANYONE HELP ME WITH THIS?
    Thursday, October 28, 2010 10:46 PM
  • <br/> for linebreaks or Rich Editor in your profile may help.
    Thursday, October 28, 2010 11:54 PM
  • thanks..

    here it is reposted

    Hi I am having a problem with the root and namespace for my xml i keep getting this error. +  _innerException {"<OpenSearchDescription xmlns=''> was not expected."} System.Exception {System.InvalidOperationException}

    this is the xml i an trying to deserialize.

    url to xml http://api.themoviedb.org/2.1/Person.search/en/xml/b64bb199343edf728721342eac53a677/Brad+Pitt
    here is my code..
    [XmlRoot("OpenSearchDescription", Namespace= "http://a9.com//spec/opensearch/1.1/")]  
    public class ShowActorsInfo  
        {  
            [XmlElement("people")]  
            public GetActorsInfo GetActorInfo { getset; }  
     
            public ShowActorsInfo()  
            {  
                GetActorInfo = new GetActorsInfo();  
            }  
        }  
     
        public class GetActorsInfo  
        {  
            [XmlElement("person")]  
            public List<ShowDetailActorsInfo> Info { getset; }  
        }  
     
        public class ShowDetailActorsInfo  
        {  
     
            [XmlElement("popularity")]  
            public string Popularity { getset; }  
     
            [XmlElement("name")]  
            public string ActorName { getset; }  
     
            [XmlElement("id")]  
            public string ActorId { getset; }  
     
            [XmlElement("biography")]  
            public string Biography { getset; }  
     
            [XmlElement("images")]  
            public string actorimage { getset; }  
     
            [XmlAttribute("url")]  
            public string ActorImageUrl  
            {  
                get 
                {  
                    return actorimage;  
                }  
     
                set 
                {  
                    actorimage = value;  
                }          
            }  
     
    Friday, October 29, 2010 12:27 AM
  • can anyone provide any help??
    Friday, October 29, 2010 3:20 AM
  • Hi Celltrac,

    you`re getting this exception because the Deserialize() looks for an element named ShowActorsInfo! So there is a difference in the root element, your xml contains a root element named "OpenSearchDescription", but the Deserialize() only knows "ShowActorsInfo". It did not expect to see an element named "OpenSearchDescription", that`s what the IvalidOperationException is telling you! 

    Try it without the namespace declaration in the attribute, like so

    [XmlRoot("OpenSearchDescription")]   
    public class ShowActorsInfo   
    {   
            ... 
    }  
              

    I hope this helps
    Friday, October 29, 2010 6:54 AM
  • tried it and same error +  _innerException {"<OpenSearchDescription xmlns=''> was not expected."} System.Exception {System.InvalidOperationException}

    here is the way the xml looks

    <?xml version="1.0" encoding="UTF-8" ?>
    - <OpenSearchDescription xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
      <opensearch:Query searchTerms="Brad Pitt" />
      <opensearch:totalResults>1</opensearch:totalResults>
    - <people>
    - <person>
      <score>1</score>
      <popularity>3</popularity>
      <name>Brad Pitt</name>
      <id>287</id>
      <biography>William Bradley "Brad" Pitt (born December 18, 1963) is an American actor and film producer. Pitt has received two Academy Award nominations and four Golden Globe Award nominations, winning one. He has been described as one of the world's most attractive men, a label for which he has received substantial media attention.</biography>
      <url>http://www.themoviedb.org/person/287</url>
    - <images>
      <image type="profile" url="http://hwcdn.themoviedb.org/profiles/00e/4bc977ad017a3c182400000e/brad-pitt-original.jpg" size="original" width="1295" height="1969" id="4bc977ad017a3c182400000e" />
      <image type="profile" url="http://hwcdn.themoviedb.org/profiles/00e/4bc977ad017a3c182400000e/brad-pitt-profile.jpg" size="profile" width="185" height="281" id="4bc977ad017a3c182400000e" />
      <image type="profile" url="http://hwcdn.themoviedb.org/profiles/00e/4bc977ad017a3c182400000e/brad-pitt-thumb.jpg" size="thumb" width="45" height="68" id="4bc977ad017a3c182400000e" />
      </images>
      <version>96</version>
      <last_modified_at>2010-10-25 15:41:10</last_modified_at>
      </person>
      </people>
      </OpenSearchDescription>

    my new code.
    [XmlRoot("OpenSearchDescription")]  
        public class ShowActorsInfo  
        {  
            [XmlElement("people")]  
            public GetActorsInfo GetActorInfo { getset; }  
     
            public ShowActorsInfo()  
            {  
                GetActorInfo = new GetActorsInfo();  
            }  
        }  
     
        public class GetActorsInfo  
        {  
            [XmlElement("person")]  
            public List<ShowDetailActorsInfo> Info { getset; }  
        } 

    Friday, October 29, 2010 7:48 AM
  • Hmm,

    are you able to change the element name in the XML you`re deserializing to match the class name?

    Have not had such a problem myself and do not really know to solve this problem properly at the moment.
    Friday, October 29, 2010 8:35 AM
  • no this is a service i am using. i do not have access to changing it.
    Friday, October 29, 2010 9:08 AM
  • It appears the XML wasn't meant to be deserialized as you're trying to do. Just read it in manually. You're making it harder than it needs to be. :)
    Friday, October 29, 2010 11:38 AM
  • Hi there, Simple add the Xml namespace to the XmlElement attribute: [XmlElement(ElementName = "OpenSearchDescription", Namespace = "http://a9.com/-/spec/opensearch/1.1/")]
    Wednesday, December 29, 2010 11:31 AM