1

I have a xml file in below format

I want to fetch the value of Child1 When I am using below code it is providing a null value. Please help

XDocument xmlDoc = XDocument.Load(fileName);
 XElement po = xmlDoc.Root.Element("Root");
 XElement el1 = po.Element("Child1");
2
  • Root should be allready the root tag. Commented Jan 15, 2015 at 8:49
  • 1
    BTW, the second <Root> should of course be </Root>. Commented Jan 15, 2015 at 8:55

1 Answer 1

2

Use this:

xmlDoc.Descendants("Child1").First();
Sign up to request clarification or add additional context in comments.

2 Comments

Or xmlDoc.Descendants("Child1").First().Value if you only want the value, or also xmlDoc.Root.Element("Child1").Value
i used below code as mentioned by you string s1 = xmlDoc.Root.Element("Child1").Value;i am getting error as Object reference not set to an instance of an object.

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.