0

I have an IEnumerable<XElement> object that I want to query based on a certain node value, but continually get a null element error. How do I select the value of two elements based on the value of a searched element? I am attempting to return firstName and lastName where ID == somevalue.

Here is the structure of my XML:

<Profile>
    <ID>123</ID>
    <firstName>Not</firstName>
    <lastName>Registered</lastName>
</Profile>

Snippet of my query:

XDocument xdoc = XDocument.Load(someXml);
IEnumerable<XElement> nodes = xdoc.Root.Elements();
XNamespace ns = "xmlNamespace";

var name = nodes.Elements(ns + "firstName")
     .Where(x => (int)x.Element(ns + "ID") == 123)
     .SingleOrDefault();

I was basing my query on this article, but I still can't find a mixture that returns what I want.

UPDATE

I have tried the suggested answers below and still see no results or receive an Object reference not set to an instance of an object exception.

I am still working through the issue and am now trying this:

var result = xdoc
    .Descendants(ns + "Profile")
    .Where(x => (int)x.Element(ns + "ID") == 1)
    .Select(x => new { FirstName = (string)x.Element(ns + "firstName"), LastName = (string)x.Element(ns + "lastName") })
    .FirstOrDefault();

When stepping through this, the query hangs after running the Where clause.

5
  • Why using XNamespace here, does the actual XML has namespace? Commented Jul 3, 2015 at 1:51
  • Also which line exactly throwing null reference exception? Commented Jul 3, 2015 at 1:56
  • Is there only one element in your Xml? If no, then what's the root element, if there's any at all. Commented Jul 3, 2015 at 17:31
  • @har07 yes there is a namespace <source xmlns='http://www.mysource.com/source'> and then proceeds to have child <Profile> nodes. Commented Jul 6, 2015 at 15:50
  • @user2596756: Are you aware that you can select an answer for your questions? Commented Jul 28, 2015 at 7:30

3 Answers 3

1

I suppose "Profile" is not a root of your XML after all.

So if I am reading your mind correctly you have something like this:

Xml:

<root>
<Profile>
    <ID>123</ID>
    <firstName>Not</firstName>
    <lastName>Registered</lastName>
</Profile>
<Profile>
    <ID>124</ID>
    <firstName>A</firstName>
    <lastName>B</lastName>
</Profile>
</root>

Code to get profile section where id element equals to 123:

var result = XDocument.Parse(yourXml)
        .Root
        .Elements()
        .SingleOrDefault(i => i.Element("ID").Value == "123");
Sign up to request clarification or add additional context in comments.

2 Comments

I received an Object reference not set to an instance of an object. exception when executing line i.Element("ID").Value == "1". Please note that ID = 1 does exist in my XML schema.
I posted completely working code. But you have to adjust it for your needs, since you didn't share your xml schema and check the answer from hungndv if your xml has namespace defined.
0

I do this examples to deal with namespace:

Profiles.xml:

<Profiles xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
    <Profile>
        <ID>122</ID>
        <firstName>Not</firstName>
        <lastName>Registered</lastName>
    </Profile>
    <Profile>   
        <ID>123</ID>
        <firstName>Not</firstName>
        <lastName>Registered</lastName>
    </Profile>
</Profiles>

Code:

XElement xDoc = XElement.Load(@"C:\Profiles.xml");
XNamespace ns = "http://schemas.microsoft.com/2004/06/E2ETraceEvent";

var profile = xDoc.Elements(ns + "Profile")
            .SingleOrDefault(x => x.Element(ns + "ID").Value == "123");
Console.WriteLine("firstName: " + profile.Element(ns + "firstName").Value);
Console.WriteLine("lastName: " + profile.Element(ns + "lastName").Value);

Hope this help.

Comments

0

Your xml Like this

<?xml version="1.0" encoding="utf-8" ?>
<Employee>
  <Profile>
    <ID>123</ID>
    <firstName>Kumod</firstName>
    <lastName>Singh</lastName>
  </Profile>
  <Profile>
    <ID>124</ID>
    <firstName>Ravi</firstName>
    <lastName>Ranjam</lastName>
  </Profile>
</Employee>

We can access the first Name and Last Name

 string strPath1 = Server.MapPath("~/XmlData/abc.xml");
        var ResultOfFirLasName = (from studentProfile in XDocument.Load(strPath1).Descendants("Profile")
                                  where (int)studentProfile.Element("ID") == 123
                                  select new { FirstName = (string)studentProfile.Element("firstName"), LastName = (string)studentProfile.Element("lastName") }).FirstOrDefault();
        string strFirstName = ResultOfFirLasName.FirstName;
        string LastName = ResultOfFirLasName.LastName;

You can do with lemda expression too

var ResultOfFirLasName1 = XDocument.Load(strPath1).Descendants("Profile").Where(x => (int)x.Element("ID") == 123).Select(x=> new {FirstName =(string)x.Element("firstName"),LastName =(string)x.Element("lastName")}).FirstOrDefault();

Same way you again access the first Name and Last Name

Comments

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.