Search code examples
c#xmlxmldocument

How to fetch root level node in XML C#


I am trying to access element at first level in given xml. I am using below code to access it but it gives me nested one as it come first.

 var xml = "<grading>" +
                      "<leap>" +
                         "<controlId>1</controlId>" +
                       "</leap>" +
                     "<controlId>2</controlId>" +
                   "</grading>";

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xml);

        var node = doc.DocumentElement.SelectSingleNode("//controlId").InnerText;

It is giving me value 1 while i am trying to access value 2 which is inside root node. Do we have any by which we can access it.


Solution

  •  var node = doc.DocumentElement.SelectSingleNode("//grading/controlId").InnerText;