1

In this question, if I need to get "date" included in the output, what changes should be made?

When I included

let dt = l.Element("Date").Value

It gives, "Object reference not set to an instance of an object"

var query = from l in doc.Descendants("L1")
            let dt = l.Element("Date").Value
            let id = l.Attribute("id").Value
            from subject in l.Descendants("Subject")
            select new
            {
                Date = dt,
                Id = id,
                SubjectName = (string)subject.Attribute("SubjectName"),
                Score = (string)subject.Attribute("Score")
            };


foreach (var result in query)
{
    Console.WriteLine(result);
}
5
  • When you included that, what happened? Note that the XML that you've shown in the other question doesn't have any contents in the <date> elements. Commented May 21, 2012 at 19:14
  • 1
    he completed it with "it gives, "Object reference not set to an instance of an object" it got lumped into the code block Commented May 21, 2012 at 19:16
  • 1
    fixed the sentence (sort of). Commented May 21, 2012 at 19:21
  • Note that your xml on the previous question has <date> and you are looking for Date -- the casing of the element name needs to be consistent. Commented May 21, 2012 at 19:35
  • Lets say date is string. Tried D Stanley's and Lucas Reis suggestion but it returns { Date = , Id = 10, SubjectName = subject1, Score = A1 } { Date = , Id = 10, SubjectName = subject2, Score = A2 } { Date = , Id = 10, SubjectName = subject2, Score = A3 } { Date = , Id = 20, SubjectName = subject4, Score = B1 } { Date = , Id = 20, SubjectName = subject5, Score = B2 } { Date = , Id = 20, SubjectName = subject6, Score = B3 } Commented May 21, 2012 at 19:36

3 Answers 3

2

If l doesn't have an Date element, trying to access l.Element("Date").Value will incur an error. You can use a conditional:

var query = from l in doc.Descendants("L1")
        let dt = l.Elements("date").Any() 
                 ? l.Element("date").Value
                 : AnyDefaultValueIWantForDate
        let id = l.Attribute("id").Value
        from subject in l.Descendants("Subject")
        select new
        {
            Date = dt,
            Id = id,
            SubjectName = subject.Attribute("SubjectName").Value,
            Score = subject.Attribute("Score").Value
        };

(I also added the .Value in SubjectName and Score).

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

3 Comments

Is there any data in the Date element in the XML? Because in your link where you show a part of it, there is none...
please use this for testing <Root> <L1 id="10"> <date>2012Jan01</date> <Subject SubjectName="subject1" Score="A1"/> <Subject SubjectName="subject2" Score="A2"/> <Subject SubjectName="subject2" Score="A3"/> </L1> <L1 id="20"> <date>2012Jan02</date> <Subject SubjectName="subject4" Score="B1"/> <Subject SubjectName="subject5" Score="B2"/> <Subject SubjectName="subject6" Score="B3"/> </L1> </Root>
edited: changed "Date" for "date" (XML is case sensitive).
1

Looking at your other XML, it does not, as Mr. Skeet mentioned, have anything in the <date> elements. You will need to explicitly handle that if you aren't planning on always having data in there.

You can do something like this:

let dt = l.Element("date") == null ? string.Empty : l.Element("date").Value 

Comments

1

Assuming l is not null then l.Element("Date") is null which would mean that one or more of your L1 elements does not have a child Date element.

The fix would depend on what you want to do with missing dates. If you want to use default(DateTime) as a "magic" date, you could do:

let dt = (l.Element("Date") == null ? default(DateTime) : l.Element("Date").Value)

1 Comment

One problem with that unary is that the default datetime will return a datetime, and the l.Element("Date").Value will return a string. There needs to be a conversion on one side or the other.

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.