user257412
user257412

Reputation: 724

C# , xml parsing. get data between tags

I have a string :

responsestring = "<?xml version="1.0" encoding="utf-8"?>
<upload><image><name></name><hash>SOmetext</hash>"

How can i get the value between

<hash> and </hash>

?

My attempts :

responseString.Substring(responseString.LastIndexOf("<hash>") + 6, 8); // this sort of works , but won't work in every situation.

also tried messing around with xmlreader , but couldn't find the solution.

ty

Upvotes: 3

Views: 10458

Answers (6)

Johnny Wu
Johnny Wu

Reputation: 1528

XmlReader_Object.ReadToFollowing("hash");

string value = XmlReader_Object.ReadInnerXml();

Upvotes: 0

explorer
explorer

Reputation: 12090

Get your xml well formed and escape the double quotes with backslash. Then apply the following code

 XDocument resp = XDocument.Parse("<hash>SOmetext</hash>");

       var r= from element in resp.Elements()
           where element.Name == "hash"
           select element;


    foreach (var item in r)
    {
        Console.WriteLine(item.Value);
    }

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1502286

Others have suggested LINQ to XML solutions, which is what I'd use as well, if possible.

If you're stuck with .NET 2.0, use XmlDocument or even XmlReader.

But don't try to manipulate the raw string yourself using Substring and IndexOf. Use an XML API of some description. Otherwise you will get it wrong. It's a matter of using the right tool for the job. Parsing XML properly is a significant chunk of work - work that's already been done.

Now, just to make this a full answer, here's a short but complete program using your sample data:

using System;
using System.Xml.Linq;

class Test
{
    static void Main()
    {
        string response = @"<?xml version='1.0' encoding='utf-8'?>
<upload><image><name></name><hash>Some text</hash></image></upload>";

        XDocument doc = XDocument.Parse(response);

        foreach (XElement hashElement in doc.Descendants("hash"))
        {
            string hashValue = (string) hashElement;
            Console.WriteLine(hashValue);
        }
    }
}

Obviously that will loop over all the hash elements. If you only want one, you could use doc.Descendants("hash").Single() or doc.Descendants("hash").First() depending on your requirements.

Note that both the conversion I've used here and the Value property will return the concatenation of all text nodes within the element. Hopefully that's okay for you - or you could get just the first text node which is a direct child if necessary.

Upvotes: 7

Kangkan
Kangkan

Reputation: 15571

You can use an xmlreader and/or xpath queries to get all desired data.

Upvotes: 0

Vinay B R
Vinay B R

Reputation: 8421

Try

XDocument doc = XDocument.Parse(str);
var a = from hash in doc.Descendants("hash")
        select hash.Value;

you will need System.Core and System.Xml.Linq assembly references

Upvotes: 9

Zephyr
Zephyr

Reputation: 7823

var val = XElement.Parse();

val.Descendants(...).Value

Upvotes: 1

Related Questions