{"<用户 xmlns=''>不是预期的.} 反序列化 Twitter XML [英] {"<user xmlns=''> was not expected.} Deserializing Twitter XML
问题描述
我正在通过 OAuth 从 Twitter 中提取 XML.
我正在请求 http://twitter.com/account/verify_credentials.xml,它返回以下 XML:
<用户><id>16434938</id><name>Lloyd Sparkes</name><screen_name>lloydsparkes</screen_name><location>英国埃塞克斯郡霍克利</location><描述>学生</描述><profile_image_url>http://a3.twimg.com/profile_images/351849613/twitterProfilePhoto_normal.jpg</profile_image_url><url>http://www.lloydsparkes.co.uk</url><protected>false</protected><followers_count>115</followers_count><profile_background_color>9fdaf4</profile_background_color><profile_text_color>000000</profile_text_color><profile_link_color>220f7b</profile_link_color><profile_sidebar_fill_color>FFF7CC</profile_sidebar_fill_color><profile_sidebar_border_color>F2E195</profile_sidebar_border_color><friends_count>87</friends_count><created_at>9 月 24 日星期三 14:26:09 +0000 2008</created_at><favourites_count>0</favourites_count><utc_offset>0</utc_offset><time_zone>伦敦</time_zone><profile_background_image_url>http://s.twimg.com/a/1255366924/images/themes/theme12/bg.gif</profile_background_image_url><profile_background_tile>false</profile_background_tile><statuses_count>1965</statuses_count><notifications>false</notifications><geo_enabled>false</geo_enabled><verified>false</verified><following>false</following><状态><created_at>Mon Oct 12 19:23:47 +0000 2009</created_at><id>4815268670</id><文本>»@alexmuller 你在开玩笑吗?它应该都是黑色领带"着装要求<source><a href="http://code.google.com/p/wittytwitter/"rel=nofollow">Witty</a></source><截断>假</截断><in_reply_to_status_id>4815131457</in_reply_to_status_id><in_reply_to_user_id>8645442</in_reply_to_user_id><收藏夹>假</收藏夹><in_reply_to_screen_name>alexmuller</in_reply_to_screen_name><地理/></状态></用户>
我正在使用以下代码进行反序列化:
public User VerifyCredentials(){字符串 url = "http://twitter.com/account/verify_credentials.xml";字符串 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);}
我的 User
类有以下内容:
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]公共部分类用户{[XmlElement(ElementName = "id")]公共长 ID { 获取;放;}[XmlElement(ElementName = "name")]公共字符串名称 { 获取;放;}[XmlElement(ElementName = "screen_name")]公共字符串 ScreenName { get;放;}[XmlElement(ElementName = "location")]公共字符串位置{获取;放;}[XmlElement(ElementName = "description")]公共字符串 描述 { 获取;放;}[XmlElement(ElementName = "profile_image_url")]公共字符串 ProfileImageUrl { 获取;放;}[XmlElement(ElementName = "url")]公共字符串 Url { 获取;放;}[XmlElement(ElementName = "protected")]public bool 受保护的 { get;放;}[XmlElement(ElementName = "followers_count")]公共 int FollowerCount { 获取;放;}[XmlElement(ElementName = "profile_background_color")]公共字符串 ProfileBackgroundColor { 获取;放;}[XmlElement(ElementName = "profile_text_color")]公共字符串 ProfileTextColor { 获取;放;}[XmlElement(ElementName = "profile_link_color")]公共字符串 ProfileLinkColor { 获取;放;}[XmlElement(ElementName = "profile_sidebar_fill_color")]公共字符串 ProfileSidebarFillColor { 获取;放;}[XmlElement(ElementName = "profile_sidebar_border_color")]公共字符串 ProfileSidebarBorderColor { 获取;放;}[XmlElement(ElementName = "friends_count")]公共 int FriendsCount { 得到;放;}[XmlElement(ElementName = "created_at")]公共字符串 CreatedAt { 获取;放;}[XmlElement(ElementName = "favourties_count")]public int FavouritesCount { get;放;}[XmlElement(ElementName = "utc_offset")]公共 int UtcOffset { 获取;放;}[XmlElement(ElementName = "time_zone")]公共字符串时区{获取;放;}[XmlElement(ElementName = "profile_background_image_url")]公共字符串 ProfileBackgroundImageUrl { 获取;放;}[XmlElement(ElementName = "profile_background_tile")]公共布尔 ProfileBackgroundTile { 获取;放;}[XmlElement(ElementName = "statuese_count")]公共 int StatusesCount { 获取;放;}[XmlElement(ElementName = "通知")]公共字符串通知 { 获取;放;}[XmlElement(ElementName = "geo_enabled")]public bool GeoEnabled { get;放;}[XmlElement(ElementName = 已验证")]公共 bool 已验证 { 获取;放;}[XmlElement(ElementName = "following")]公共字符串 跟随 { get;放;}[XmlElement(ElementName = "status", IsNullable=true)]公共状态 CurrentStatus { 获取;放;}}
但是当它反序列化上述 XML 时,应用程序会抛出以下内容:
<块引用>$exception {XML 文档 (2, 2) 中存在错误."} System.Exception {System.InvalidOperationException}
<块引用>
InnerException {"<user xmlns=''>不是预期的."} System.Exception {System.InvalidOperationException}
现在我已经四处搜索,我能找到的最佳解决方案是在序列化内容时向序列化程序添加空白命名空间,但我没有序列化它,所以我不能.
我还有一些用于接收状态的代码,效果很好.
那么有人可以向我解释为什么会发生错误吗?以及可能的解决方案?
要么使用 XmlRoot 属性装饰您的根实体,该属性将在编译时使用.
[XmlRoot(Namespace = "www.contoso.com", ElementName = "MyGroupName", DataType = "string", IsNullable=true)]
或者在运行时反序列化时指定根属性.
XmlRootAttribute xRoot = new XmlRootAttribute();xRoot.ElementName = "用户";//xRoot.Namespace = "http://www.cpandl.com";xRoot.IsNullable = true;XmlSerializer xs = new XmlSerializer(typeof(User),xRoot);
I'm pulling in the XML from Twitter via OAuth.
I'm doing a request to http://twitter.com/account/verify_credentials.xml, which returns the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<user>
<id>16434938</id>
<name>Lloyd Sparkes</name>
<screen_name>lloydsparkes</screen_name>
<location>Hockley, Essex, UK</location>
<description>Student</description>
<profile_image_url>http://a3.twimg.com/profile_images/351849613/twitterProfilePhoto_normal.jpg</profile_image_url>
<url>http://www.lloydsparkes.co.uk</url>
<protected>false</protected>
<followers_count>115</followers_count>
<profile_background_color>9fdaf4</profile_background_color>
<profile_text_color>000000</profile_text_color>
<profile_link_color>220f7b</profile_link_color>
<profile_sidebar_fill_color>FFF7CC</profile_sidebar_fill_color>
<profile_sidebar_border_color>F2E195</profile_sidebar_border_color>
<friends_count>87</friends_count>
<created_at>Wed Sep 24 14:26:09 +0000 2008</created_at>
<favourites_count>0</favourites_count>
<utc_offset>0</utc_offset>
<time_zone>London</time_zone>
<profile_background_image_url>http://s.twimg.com/a/1255366924/images/themes/theme12/bg.gif</profile_background_image_url>
<profile_background_tile>false</profile_background_tile>
<statuses_count>1965</statuses_count>
<notifications>false</notifications>
<geo_enabled>false</geo_enabled>
<verified>false</verified>
<following>false</following>
<status>
<created_at>Mon Oct 12 19:23:47 +0000 2009</created_at>
<id>4815268670</id>
<text>» @alexmuller your kidding? it should all be "black tie" dress code</text>
<source><a href="http://code.google.com/p/wittytwitter/" rel="nofollow">Witty</a></source>
<truncated>false</truncated>
<in_reply_to_status_id>4815131457</in_reply_to_status_id>
<in_reply_to_user_id>8645442</in_reply_to_user_id>
<favorited>false</favorited>
<in_reply_to_screen_name>alexmuller</in_reply_to_screen_name>
<geo/>
</status>
</user>
I am using the following code to deserialize:
public User VerifyCredentials()
{
string url = "http://twitter.com/account/verify_credentials.xml";
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 it's deserializing the above XML the application throws the following:
$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 i'm not serializing it so I can't.
I also have some code for receiving Statuses, which works fine.
So can someone explain to me why the error is happening? As well as a possible solution?
Either decorate your root entity with the XmlRoot attribute which will be used at compile time.
[XmlRoot(Namespace = "www.contoso.com", 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 = "http://www.cpandl.com";
xRoot.IsNullable = true;
XmlSerializer xs = new XmlSerializer(typeof(User),xRoot);
这篇关于{"<用户 xmlns=''>不是预期的.} 反序列化 Twitter XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!