10

Here's the XML string.

<?xml version="1.0" encoding="utf-16"?>
<questionresponses>
  <question id="dd7e3bce-57ee-497a-afe8-e3d8d25e2671">
    <text>Question 1?</text>
    <response>abcdefg</response>
    <correctresponse>123</correctresponse>
  </question>
  <question id="efc43b1d-048f-4ba9-9cc0-1cc09a7eeaf2">
    <text>Question 2?</text>
    <response>12345678</response>
    <correctresponse>123</correctresponse>
  </question>
</questionresponses>

So how could I get value of <response> element by given question Id? Say, if I give id value = "dd7e3bce-57ee-497a-afe8-e3d8d25e2671", I'd like to have string value abcdefg returned as result.

var xmlstr = "content from above xml example";
using (var reader = XmlReader.Create(new StringReader(xmlstr)))
{
    while(reader.Read())
    {
        if(reader.IsStartElement())
        {
            var attr = reader["id"];
            if(attr != null && attr == "dd7e3bce-57ee-497a-afe8-e3d8d25e2671")
            {
                if(reader.ReadToDescendant("response"))
                {
                    result = reader.Value; // <= getting empty string? So what's wrong?
                    break;
                }
            }
        }
    }
}
woodykiddy
  • 6,074
  • 16
  • 59
  • 100

4 Answers4

24

you might need to do like this , problem i think is reader is not moving to text and because of that you are getting empty

        if(reader.ReadToDescendant("response"))
            {
                reader.Read();//this moves reader to next node which is text 
                result = reader.Value; //this might give value than 
                break;
            }

Above one is working for me you can try out at your end

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • 1
    Yeah, indeed, I was missing out reader.Read() which is why reader.Value is empty. Thanks for point this out quickly. – woodykiddy Sep 20 '13 at 01:48
  • Can you please see one of my [question](https://stackoverflow.com/questions/55430357/how-to-get-xml-web-service-response-in-c-sharp) regarding the xml response? – Moeez Mar 31 '19 at 05:24
  • Do we need `break;` here? – Corey Aug 07 '20 at 09:06
5

I would use LINQ2XML..

XDocument doc=XDocument.Parse(xmlstr);
String response=doc.Elements("question")
                   .Where(x=>x.Attribute("id")==id)
                   .Single()
                   .Element("response")
                   .Value;
Anirudha
  • 32,393
  • 7
  • 68
  • 89
1

You can use this function to get a response for specific questions from XML stored in QuestionXML.xml.

private string getResponse(string questionID)
            {
                string response = string.Empty;
                using (StreamReader sr = new StreamReader("QuestionXML.xml", true))
                {
                    XmlDocument xmlDoc1 = new XmlDocument();
                    xmlDoc1.Load(sr);
                    XmlNodeList itemNodes = xmlDoc1.GetElementsByTagName("question");
                    if (itemNodes.Count > 0)
                    {
                        foreach (XmlElement node in itemNodes)
                        {
                            if (node.Attributes["id"].Value.ToString() == questionID.Trim())
                            {
                                response = node.SelectSingleNode("response").InnerText;
                                break;
                            }

                        }
                    }
                }
                return response;
            }
Community
  • 1
  • 1
Rezoan
  • 1,745
  • 22
  • 51
1
if (reader.NodeType == XmlNodeType.Element)
{
    if(reader.Name == "response")
    {
        reader.read();
        var res = reader.Value;
    }
} 

//it works for me !!!!

Nick Volynkin
  • 14,023
  • 6
  • 43
  • 67