我正在尝试使用以下代码检索“标题”的值:
private void GetTweets_Click(object sender, RoutedEventArgs e)
{
WebClient client = new WebClient();
client.DownloadStringCompleted += (s, ea) =>
{
XDocument doc = XDocument.Parse(ea.Result);
XNamespace ns = "http://www.w3.org/2005/Atom";
var items = from item in doc.Descendants(ns + "entry")
select new Tweet()
{
Title = item.Element(ns + "title").Value,
Image = new Uri((from XElement xe in item.Descendants(ns + "link")
where xe.Attribute("type").Value == "image/png"
select xe.Attribute("href").Value).First<string>()),
};
foreach (Tweet t in items)
{
_tweets.Add(t);
}
};
client.DownloadStringAsync(new Uri("https://twitter.com/statuses/user_timeline/[username].atom?count=10"));
}
我能够检索推文列表,但是,我想删除“标题”值显示的前16个字符。
这里有没有办法使用子字符串函数? 谢谢。
答案 0 :(得分:2)
假设item.Element(ns + "title").Value
是string
,您应该可以使用String.Substring(Int32) method。
Title = item.Element(ns + "title").Value.Substring(16),
请注意,如果Title的长度小于16个字符,这将抛出异常,因此最好先测试它。
Title = item.Element(ns + "title").Value.Length > 16
? item.Element(ns + "title").Value.Substring(16)
: item.Element(ns + "title").Value,