Click here to Skip to main content
15,943,008 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
var loadv = XDocument.Load("C:/Users/DELL/Desktop/test.xml").ToString();
            var xDocv = XDocument.Parse(loadv);
            string NbV = xDocv.Descendants("UserName").First().Value;

I encountered an error "Sequence contains no elements" , this my xml file and I dont know why the sequence contains no element.
however "xDocv" contains the xml sequence

<?xml version="1.0" encoding="utf-8"?>
<QueriesResponse xmlns:xsi="" xmlns:xsd="">
  <Jobs xmlns="">
    <Job allowExpiredUpdates="false" enforceUniqueness="true" inventoryType="transactional" jobComplete="true" jobRefCode="testtest">
      <RecruiterReference userId="204841377">

There is someone who can check this with me ?

1 solution


You need to search for the tag "UserName" in that namespace:
string NbV = xDocv.Descendants("UserName").First().Value;

XNamespace ns = "";
string NbV = xDocv.Descendants(ns + "UserName").First().Value;

Optional information:

Furthermore, instead of this:
string NbV = xDocv.Descendants(ns + "UserName").First().Value;

it might be better to use this:
string NbV = (string)xDocv.Descendants(ns + "UserName").FirstOrDefault();

Why? If the tag would be missing, then you could avoid getting an error "Sequence contains no elements".
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900