3

I have a XML file like below:

<report timestamp="3201" reportVersion="2" request="3981135340">
<question timedOut="false" time="3163" attempts="2" correct="true" id="13"> 
<answer status="attempt"> 
<radioButton correct="false" value="true" id="17" /> 
</answer> 
<answer status="correct"> 
<radioButton correct="true" value="true" id="15" /> 
</answer> 
</question> 
</report>

I want to read the child nodes based on 'status' attribute of 'answer' node.

0

4 Answers 4

4

Use XmlReader (fastest but forward only), XDocument (LINQ to XML) or XmlDocument. See the examples in the msdn documentation.

Sign up to request clarification or add additional context in comments.

Comments

1

Using LINQ to XML:

using System.Xml.Linq;

var doc = XDocument.Parse(xml); // or XDocument.Load()
var elements = from e in doc.Descendants("answer")
               where e.Attribute("status").Value == "attempt"
               select e;

// elements will be IEnumerable<XElement>

Comments

0

Use XmlDocument and XPath:

XmlDocument document = new XmlDocument();
//here you should load your xml for example with document.Load();
XmlNodeList nodes = document.SelectNodes("/report/question/answer[@status = 'correct']/radioButton");

Just modify the XPath to your needs.

Comments

0

try this one..

foreach (XmlNode xnode in xdoc.SelectNodes("report/question/answer"))
    {
        if (xnode.Attributes.GetNamedItem("status").Value == "correct")
        {
            string value = xdoc.SelectSingleNode("report/question/answer[@status='correct']/radioButton").Attributes.GetNamedItem("id").Value;

        }
        if (xnode.Attributes.GetNamedItem("status").Value == "attempt")
        {
            string value = xdoc.SelectSingleNode("report/question/answer[@status='attempt']/radioButton").Attributes.GetNamedItem("id").Value;
        }
    }

Comments

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.