gpt4 book ai didi

c# - Linq to XML 从 twitter API 解析单个 "status"节点

转载 作者:太空宇宙 更新时间:2023-11-03 22:08:55 26 4
gpt4 key购买 nike

好的,我正在构建 Windows Phone Twitter 应用程序,但出于某种原因我无法解析这些数据。我只是想解析出一个单一的状态...

这是来自 twitter 的 xml 数据...

<status>
<created_at>Sat Sep 10 17:59:12 +0000 2011</created_at>
<id>112585933307645952</id>
<text>AP: Start 'em/Sit 'em Week 1 - Arrowhead Pride (blog): Midwest Sports FansAP: Start 'em/Sit 'em Week 1Arrowhead ... http://t.co/rWnx5pe</text>
<source>&lt;a href="http://twitterfeed.com" rel="nofollow"&gt;twitterfeed&lt;/a&gt;</source>
<truncated>false</truncated>
<favorited>false</favorited>
<in_reply_to_status_id></in_reply_to_status_id>
<in_reply_to_user_id></in_reply_to_user_id>
<in_reply_to_screen_name></in_reply_to_screen_name>
<retweet_count>0</retweet_count>
<retweeted>false</retweeted>
<user>
<id>27680614</id>
<name>Fantasy Football</name>
<screen_name>hackhype</screen_name>
<location>Atlanta, GA</location>
<description>NFL News and Fantasy Perspective!</description>
<profile_image_url>http://a1.twimg.com/profile_images/509176461/icon_normal.gif</profile_image_url>
<profile_image_url_https>https://si0.twimg.com/profile_images/509176461/icon_normal.gif</profile_image_url_https>
<url>http://www.facebook.com/hackhype</url>
<protected>false</protected>
<followers_count>29888</followers_count>
<profile_background_color>ebebeb</profile_background_color>
<profile_text_color>333333</profile_text_color>
<profile_link_color>0084B4</profile_link_color>
<profile_sidebar_fill_color>ebebeb</profile_sidebar_fill_color>
<profile_sidebar_border_color>040470</profile_sidebar_border_color>
<friends_count>6789</friends_count>
<created_at>Mon Mar 30 17:01:37 +0000 2009</created_at>
<favourites_count>1</favourites_count>
<utc_offset>-18000</utc_offset>
<time_zone>Quito</time_zone>
<profile_background_image_url>http://a2.twimg.com/profile_background_images/44228452/twitterbackground.jpg</profile_background_image_url>
<profile_background_image_url_https>https://si0.twimg.com/profile_background_images/44228452/twitterbackground.jpg</profile_background_image_url_https>
<profile_background_tile>false</profile_background_tile>
<profile_use_background_image>true</profile_use_background_image>
<notifications>false</notifications>
<geo_enabled>false</geo_enabled>
<verified>false</verified>
<following>true</following>
<statuses_count>10219</statuses_count>
<lang>en</lang>
<contributors_enabled>false</contributors_enabled>
<follow_request_sent>false</follow_request_sent>
<listed_count>466</listed_count>
<show_all_inline_media>false</show_all_inline_media>
<default_profile>false</default_profile>
<default_profile_image>false</default_profile_image>
<is_translator>false</is_translator>
</user>
<geo />
<coordinates />
<place />
<possibly_sensitive>false</possibly_sensitive>
<contributors />
<entities>
<user_mentions />
<urls>
<url end="135" start="116">
<url>http://t.co/rWnx5pe</url>
<display_url>bit.ly/ookpnp</display_url>
<expanded_url>http://bit.ly/ookpnp</expanded_url>
</url>
</urls>
<hashtags />
</entities>
</status>

这是我用来解析它的代码(它不工作)。它正在编译和运行,但是“thisTweet”返回为 null....

XElement xmlData = XElement.Parse(e.Result);

thisTweet = (from tweet in xmlData.Descendants("status")
select new Tweet
{
created_at = tweet.Element("created_at").Value,
text = tweet.Element("text").Value,

//user info
name = tweet.Element("user").Element("name").Value,
profile_image_url = tweet.Element("user").Element("profile_image_url").Value,
screen_name = tweet.Element("user").Element("screen_name").Value,
user_id = tweet.Element("user").Element("id").Value
}).First<Tweet>();

DataContext = thisTweet;

最佳答案

您的 XML 没有名为“status”的后代 - status 是根元素。无论如何,您都希望解析一条单个推文,那么为什么不只是:

XElement tweet = XElement.Parse(e.Result);
var thisTweet = new Tweet()
{
created_at = tweet.Element("created_at").Value,
text = tweet.Element("text").Value,

//user info
name = tweet.Element("user").Element("name").Value,
profile_image_url = tweet.Element("user").Element("profile_image_url").Value,
screen_name = tweet.Element("user").Element("screen_name").Value,
user_id = tweet.Element("user").Element("id").Value

};

关于c# - Linq to XML 从 twitter API 解析单个 "status"节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7373656/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com