Coder Perfect

‘user xmlns=”> was not anticipated. Twitter XML deserialization


I’m using OAuth to get the XML from Twitter.

I’m sending a request to credentials.xml, and I get the following XML back:

<?xml version="1.0" encoding="UTF-8"?>
  <name>Lloyd Sparkes</name>
  <location>Hockley, Essex, UK</location>
  <created_at>Wed Sep 24 14:26:09 +0000 2008</created_at>
    <created_at>Mon Oct 12 19:23:47 +0000 2009</created_at>
    <text>&#187; @alexmuller your kidding? it should all be &quot;black tie&quot; dress code</text>
    <source>&lt;a href=&quot;; rel=&quot;nofollow&quot;&gt;Witty&lt;/a&gt;</source>

To deserialize, I use the following code:

    public User VerifyCredentials()
        string url = "";
        string xml = _oauth.oAuthWebRequestAsString(oAuthTwitter.Method.GET, url, null);

        XmlSerializer xs = new XmlSerializer(typeof(User),"");

        MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(xml));

        return (User)xs.Deserialize(ms);

And for my User class, I have the following:

 [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class User

    [XmlElement(ElementName = "id")]       
    public long Id { get; set; }

    [XmlElement(ElementName = "name")] 
    public string Name { get; set; }

    [XmlElement(ElementName = "screen_name")]       
    public string ScreenName { get; set; }

    [XmlElement(ElementName = "location")]       
    public string Location { get; set; }

    [XmlElement(ElementName = "description")]      
    public string Description { get; set; }

    [XmlElement(ElementName = "profile_image_url")]      
    public string ProfileImageUrl { get; set; }

    [XmlElement(ElementName = "url")]       
    public string Url { get; set; }

    [XmlElement(ElementName = "protected")]      
    public bool Protected { get; set; }

    [XmlElement(ElementName = "followers_count")]      
    public int FollowerCount { get; set; }

    [XmlElement(ElementName = "profile_background_color")]       
    public string ProfileBackgroundColor { get; set; }

    [XmlElement(ElementName = "profile_text_color")]       
    public string ProfileTextColor { get; set; }

    [XmlElement(ElementName = "profile_link_color")]       
    public string ProfileLinkColor { get; set; }

    [XmlElement(ElementName = "profile_sidebar_fill_color")]       
    public string ProfileSidebarFillColor { get; set; }

    [XmlElement(ElementName = "profile_sidebar_border_color")]      
    public string ProfileSidebarBorderColor { get; set; }

    [XmlElement(ElementName = "friends_count")]     
    public int FriendsCount { get; set; }

    [XmlElement(ElementName = "created_at")]     
    public string CreatedAt { get; set; }

    [XmlElement(ElementName = "favourties_count")]      
    public int FavouritesCount { get; set; }

    [XmlElement(ElementName = "utc_offset")]      
    public int UtcOffset { get; set; }

    [XmlElement(ElementName = "time_zone")]       
    public string Timezone { get; set; }

    [XmlElement(ElementName = "profile_background_image_url")]        
    public string ProfileBackgroundImageUrl { get; set; }

    [XmlElement(ElementName = "profile_background_tile")]        
    public bool ProfileBackgroundTile { get; set; }

    [XmlElement(ElementName = "statuese_count")]        
    public int StatusesCount { get; set; }

    [XmlElement(ElementName = "notifications")]       
    public string Notifications { get; set; }

    [XmlElement(ElementName = "geo_enabled")]       
    public bool GeoEnabled { get; set; }

    [XmlElement(ElementName = "Verified")]        
    public bool Verified { get; set; }

    [XmlElement(ElementName = "following")]
    public string Following { get; set; }

    [XmlElement(ElementName = "status", IsNullable=true)]
    public Status CurrentStatus { get; set; }


The program, however, generates the following error when deserializing the above XML:

Now, I’ve looked around and the best solution I’ve seen is to add blank namespaces to the serializer while serializing the content, but I’m not serializing it, so that’s not an option for me.

I also have some code that works for receiving Statuses.

So, could someone perhaps explain why this issue occurs? Also, could there be a solution?

Asked by lloydsparkes

Solution #1

Either utilize the XmlRoot attribute to decorate your root object, which will be used at compile time.

[XmlRoot(Namespace = "", ElementName = "MyGroupName", DataType = "string", IsNullable=true)]

Alternatively, when de serializing at runtime, mention the root attribute.

XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "user";
// xRoot.Namespace = "";
xRoot.IsNullable = true;

XmlSerializer xs = new XmlSerializer(typeof(User),xRoot);

Answered by david valentine

Solution #2

Add the following annotations to the top of your class to make it even easier:

[Serializable, XmlRoot("user")]
public partial class User

Answered by Rebecca

Solution #3

XmlSerializer xs = new XmlSerializer(typeof(User), new XmlRootAttribute("yourRootName")); 

Answered by Ranadheer Reddy

Solution #4

Because the error message is so ambiguous, I had to use the following code:

var streamReader = new StreamReader(response.GetResponseStream());
var xmlSerializer = new XmlSerializer(typeof(aResponse));
theResponse = (bResponse) xmlSerializer.Deserialize(streamReader);

Although xmlSerializer is initialized with aResponse, I unintentionally cast it to bResponse when deserializing.

Answered by Jeremy Thompson

Solution #5

The simplest and most effective way is to utilize the XMLRoot attribute in the class you want to deserialize.


[XmlRoot(ElementName = "YourPreferableNameHere")]
public class MyClass{

Use the following Assemblies as well:

using System.Xml.Serialization;

Answered by Khawaja Asim

Post is based on