如何从xml中提取用户数据并将其发送到字符串?

时间:2012-08-02 15:14:59

标签: c# twitter oauth

http://www.voiceoftech.com/swhitley/index.php/2009/03/twitter-oauth-with-net/

url = "http://twitter.com/account/verify_credentials.xml";
xml = oAuth.oAuthWebRequest(oAuthTwitter.Method.GET, url, String.Empty);
apiResponse.InnerHtml = Server.HtmlEncode(xml);

如何从xml中提取用户数据并将其发送到字符串? 即将xml转换为字符串。

string name =
string id=

2 个答案:

答案 0 :(得分:1)

这样的事可能吗?

XDocument doc = XDocument.Parse("<insert xml here or use variable>");
var username = doc.XPathSelectElement("/SomeNode/SomeOtherNode/@username").Value;
var id = doc.XPathSelectElement("/SomeNode/SomeOtherNode/@id").Value;

答案 1 :(得分:1)

我知道你使用的是.NET 2.0,所以我在这个例子中使用了XmlDocument:

System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
TwitterUser user = new TwitterUser();

string url = "http://api.twitter.com/1/account/verify_credentials.xml";
string xml = oAuth.oAuthWebRequest(oAuthTwitter.Method.GET, url, String.Empty);

xmlDoc.LoadXml(xml);

user.id = xmlDoc.SelectSingleNode("user/id").InnerText;
user.screen_name = xmlDoc.SelectSingleNode("user/screen_name").InnerText;
user.name = xmlDoc.SelectSingleNode("user/name").InnerText;