view raw
lloydsparkes lloydsparkes - 7 months ago 26
C# Question

{"<user xmlns=''> was not expected.} Deserializing Twitter XML

So im pulling in the XML from Twitter via OAuth

So Im doing a request to

Which returns the following XML

<?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>

I am using the following code to Deserialise

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 i have the following for my User Class

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


But when its deserialising the above Xml i get the following Error thrown

  • $exception {"There is an error in XML document (2, 2)."} System.Exception {System.InvalidOperationException}

  • InnerException {"<user xmlns=''> was not expected."} System.Exception {System.InvalidOperationException}

Now i have searched around and the best solution i can find is to add blank namespaces to the serializer, when you serialize the content, BUT im not serializing it so i cant.

I also have some code for recieving Statuses, which works fine.

So can someone explain to me what and why the error is happening? As well as a possible solution?

Thanks in advance


Either decorate your root entity with the XmlRoot attribute which will be used at compile time.

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

Or specify the root attribute when de serializing at runtime.

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

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