简体   繁体   中英

parsing XML returned from server

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM