开发者

parsing XML returned from server

开发者 https://www.devze.com 2022-12-21 23:37 出处:网络
<rsp stat=\"ok\"> <image_hash>ducex</image_hash> <delete_hash>QXHbCECmDX</delete_hash>
<rsp stat="ok">

<image_hash>ducex</image_hash>
<delete_hash>QXHbCECmDX</delete_hash>
<original_image>http://i.imgur.com/ducex.jpg</original_image>
<large_thumbnail>http://i.imgur.com/ducexl.jpg</large_thumbnail>
<small_thumbnail>http://i.imgur.com/ducexs.jpg</small_thumbnail>
<imgur_page>http://imgur.com/ducex</imgur_page>
<delete_page>http://imgur.com/delete/QXHbCECmDX</delete_page>

</rsp>

First off all, can someo开发者_开发技巧ne help me start of how to parse this? All i need to do is check the "stat" value. if thats okay , then I need to get the "orginal image" link. I am targetting .NET 4.0 Client Frameowkr however so does that give me access to LINQ to XML?

How would I accomplish this using C#? Any tips to start me off with? Thanks


You can use LINQ to XML. The xmlInput variable below would contain your string.

string xmlInput = @"<rsp stat=""ok"">
<image_hash>ducex</image_hash>
<delete_hash>QXHbCECmDX</delete_hash>
<original_image>http://i.imgur.com/ducex.jpg</original_image>
<large_thumbnail>http://i.imgur.com/ducexl.jpg</large_thumbnail>
<small_thumbnail>http://i.imgur.com/ducexs.jpg</small_thumbnail>
<imgur_page>http://imgur.com/ducex</imgur_page>
<delete_page>http://imgur.com/delete/QXHbCECmDX</delete_page>
</rsp>
";

var xml = XElement.Parse(xmlInput);
if (xml.Attribute("stat").Value == "ok")
{
    string originalImage = xml.Element("original_image").Value;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号