0
<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

3
  • By returned from server do you mean you are parsing this in the HTML or are you parsing it one the server in C#. If C# you want to use the xmldoc object Commented Mar 7, 2010 at 3:50
  • I think thats irrevelant. I have a string that has that value. I dont think it matters where the string is coming from, however the string will always be in the same structure. Commented Mar 7, 2010 at 3:52
  • it's not irrelevant. Coming from a server, you may be given a Stream or an XmlReader. When you have something richer than a string, you should use it. You should avoid processing XML from strings whenever possible. Commented Mar 7, 2010 at 4:02

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, my mistake was XElement.Parse(xmlInput); I was trying to use LINQ to XML on a string variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.