1

Let us consider the following xml as

<?xml version="1.0" encoding="UTF-8" ?>
   <response success="true">
       <struct>value</struct>
   </response>

while parsing i am getting following error as

Root element is missing.

the code which i used was

foreach (XElement carselement in xdoc.Descendants("response"))
                {
                  String  value= carselement.Element("struct").Value;

                }

waiting for your solutions

4
  • 3
    How is xdoc loaded and what does it really contain? I would imagine it does not represent that XML. Commented Jun 21, 2013 at 17:44
  • @user2246674 ya thats the problem i am facing can i do it some other way Commented Jun 21, 2013 at 17:45
  • 1
    @GowthamanSS your code is working without problems. Commented Jun 21, 2013 at 18:01
  • @all thanks for your support i have missed XDocument.Parse Commented Jun 24, 2013 at 13:35

2 Answers 2

1

The XML input is not as expected (it is "empty") and the exception occurs during XDocument.Load (or XDocument.Parse, etc).

Ultimately xdoc does not contain what is expected - and the "suspect" lines never even run; again, this Exception is caused when the XML is parsed, not when it is enumerated/navigated. This scenario should be easily identified with an attached debugger or stack-trace.

Here is some minimal code that can be run in LINQPad as C# statements. I've modified it just enough to display nicely with dump. Note that it runs as expected.

var xmlStr = @"<?xml version=""1.0"" encoding=""UTF-8"" ?>
   <response success=""true"">
       <struct>value</struct>
   </response>";
var xdoc = XDocument.Parse(xmlStr);
xdoc.Descendants("response")
    .Select(e => e.Element("struct").Value)
    .Dump();

Here is how the exception can be caused (and it has nothing to do with Descendants or other enumeration/navigation):

var xmlStr = @"<?xml version=""1.0"" encoding=""UTF-8"" ?>";
var xdoc = XDocument.Parse(xmlStr);
// --> XmlException: Root element is missing
Sign up to request clarification or add additional context in comments.

1 Comment

Nicely done! That would be so easy to debug, it's not as easy to guess and figure out with a "pencil and paper"
0

Maybe your XML is over simplified, and looks like this:

<!-- example -->
<?xml version="1.0" encoding="UTF-8" ?>
<response success="true">
    <struct>value1</struct>
</response>
<response success="true">
    <struct>value2</struct>
</response>
<response success="false">
    <struct>value3</struct>
</response>

In this case you are missing a <responses></responses> which wraps around the response array of elements.

BTW, your code should work also, if your XML file is really what you quote here. Do you try to manipulate the XML too?

XDocument xdoc = XDocument.Load(filePath);
if (xdoc == null) return;

XElement response = xdoc.Descendants("response").FirstOrDefault();
XElement structElement = response.Descendants("struct").FirstOrDefault();

1 Comment

Note that multiple root elements results in "XmlException: There are multiple root elements."

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.