{“<用户xmlns =''>不是预期的。}反序列化Twitter的XML

所以即时通过OAuth从Twitter推入XML

所以我做一个请求http://twitter.com/account/verify_credentials.xml

其中返回以下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/1255366924http://img.dovov.comthemes/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 &quot;black tie&quot; dress code</text> <source>&lt;a href=&quot;http://code.google.com/p/wittytwitter/&quot; rel=&quot;nofollow&quot;&gt;Witty&lt;/a&gt;</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> 

我正在使用下面的代码来反序列化

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

我有我的用户类以下

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

但是当它反序列化上面的Xml我得到下面的错误抛出

  • $ exception {“在XML文档(2,2)中有一个错误。”} System.Exception {System.InvalidOperationException}

  • InnerException {“<用户xmlns =''>不是预期的。”} System.Exception {System.InvalidOperationException}

现在我已经search了,我可以find最好的解决scheme是添加空白的命名空间到串行器,当你序列化的内容,但我没有序列化,所以我不能。

我也有一些代码接收状态,工作正常。

那么有人可以向我解释什么是错误? 以及一个可能的解决scheme?

提前致谢

可以用编译时使用的XmlRoot属性修饰你的根实体。

 [XmlRoot(Namespace = "www.contoso.com", ElementName = "MyGroupName", DataType = "string", IsNullable=true)] 

或者在运行时序列化时指定root属性。

 XmlRootAttribute xRoot = new XmlRootAttribute(); xRoot.ElementName = "user"; // xRoot.Namespace = "http://www.cpandl.com"; xRoot.IsNullable = true; XmlSerializer xs = new XmlSerializer(typeof(User),xRoot); 

更简单的方法是将以下注释添加到类的顶部:

 [Serializable, XmlRoot("user")] public partial class User { } 
 XmlSerializer xs = new XmlSerializer(typeof(User), new XmlRootAttribute("yourRootName")); 

错误信息是如此含糊,对我来说我有这样的代码:

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

注意xmlSerializer是用aResponse实例化的,但是在反序列化过程中,我偶然将它转换为bResonse。

正如约翰·桑德斯(John Saunders)所说,检查类/属性名称是否与XML的大写字母匹配。 如果情况并非如此,问题也会发生。

最简单和最好的解决scheme就是在你的类中使用XMLRoot属性,在这个属性中你希望去除它。

喜欢:

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

还使用以下DLL

 using System.Xml.Serialization; 

我的问题是我的元素之一有xmlns属性:

 <?xml version="1.0" encoding="utf-8"?> <RETS ReplyCode="0"> <RETS-RESPONSE xmlns="blahblah"> ... </RETS-RESPONSE> </RETS> 

无论我尝试xmlns属性好像是打破了序列化程序,所以我从xml文件中删除了任何xmlns =“…”的痕迹:

 <?xml version="1.0" encoding="utf-8"?> <RETS ReplyCode="0"> <RETS-RESPONSE> ... </RETS-RESPONSE> </RETS> 

瞧! 一切正常。

我现在parsingXML文件以反序列化之前删除此属性。 不知道为什么这个工作,也许我的情况是不同的,因为包含xmlns属性的元素不是根元素。

在我的情况唯一的工作是通过使用大卫的情人节代码。 使用根Attr。 在Person类中没有帮助。

我有这个简单的Xml:

 <?xml version="1.0"?> <personList> <Person> <FirstName>AAAA</FirstName> <LastName>BBB</LastName> </Person> <Person> <FirstName>CCC</FirstName> <LastName>DDD</LastName> </Person> </personList> 

C#类:

 public class Person { public string FirstName { get; set; } public string LastName { get; set; } } 

来自Main方法的反序列化C#代码:

 XmlRootAttribute xRoot = new XmlRootAttribute(); xRoot.ElementName = "personList"; xRoot.IsNullable = true; using (StreamReader reader = new StreamReader(xmlFilePath)) { List<Person> result = (List<Person>)(new XmlSerializer(typeof(List<Person>), xRoot)).Deserialize(reader); int numOfPersons = result.Count; } 

我的问题是,根元素实际上有一个xmlns =“abc123”

所以不得不使XmlRoot(“elementname”,NameSpace =“abc123”)