2

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.

4
  • 2
    That code gives me a NullReferenceException instead (due to the use of /)... it would help if you'd provide code that demonstrates the actual problem. I'd second the recommendations to use LINQ to XML though. Commented Aug 2, 2018 at 10:14
  • Do you have to (want to) use XmlDocument and XPath? It is much easier with XDocument. Commented Aug 2, 2018 at 10:14
  • I have updated code. Now it will not give error Commented Aug 2, 2018 at 10:15
  • 2
    Right, now you've used the // that means "find the first descendant". If you just use SelectSingleNode("controlId") it will work, although I'd definitely recommend using LINQ to XML instead. Commented Aug 2, 2018 at 10:15

3 Answers 3

3

Your XPath expression is

//controlId

That will find all descendants called controlId. You only want direct children of the element you're calling the method on, so you should use

controlId

So this works:

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

However, I'd strongly recommend using LINQ to XML instead, as a cleaner XML API:

using System;
using System.Xml.Linq;

public class Program
{
    public static void Main(string[] args)
    {
        var xml = @"
<grading>
  <leap>
    <controlId>1</controlId>
  </leap>
  <controlId>2</controlId>
</grading>";

        XDocument doc = XDocument.Parse(xml);
        XElement control = doc.Root.Element("controlId");
        Console.WriteLine(control.Value); // 2
    }
}

I would avoid using XPath unless you're really trying to use its advanced functionality. To just retrieve an element, attribute etc by name, the LINQ to XML API is simpler and less error-prone (as shown by this question's existence).

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

Comments

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

Comments

0

I use alot of LINQ TO XML when i am parsing XML. For example to get the root in your set.

string xml = "";
XDocument doc = XDocument.Parse(xml);   
XElement elRoot = doc.Root

More resources can be found here: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/linq-to-xml-overview

1 Comment

I think you mean LINQ to XML rather than LINQ to SQL.

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.