Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6815831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:51:14+00:00 2026-05-26T20:51:14+00:00

Consider this XML file. Note the first Tutorial has an Author child element, and

  • 0

Consider this XML file. Note the first Tutorial has an Author child element, and the second Tutorial does not:

<?xml version="1.0" encoding="utf-8" ?>
<Tutorials>
  <Tutorial>
    <Author>The Tallest</Author>
    <Title>
      WPF Tutorial - Creating A Custom Panel Control
    </Title>
    <Date>2/18/2008</Date>
  </Tutorial>
    <Tutorial>
    <Title>
      2nd WPF Tutorial - Creating A Custom Panel Control
    </Title>
    <Date>2/18/2008</Date>
  </Tutorial>
</Tutorials>

How do I use LINQ-to-XML to load the data that is present? The code below blows up when it gets to the Tutorial section that lacks an author. I cannot figure out how to write the where statement to exclude the block that lacks an author, or how to make the code elegantly skip over the missing data. I have tried this:

where tutorial.Element("Title") != null

But the above has no effect…. Here is the problem code:

XDocument xmlDoc = XDocument.Load("C:\\xml\\2.xml");

var tutorials = from tutorial in xmlDoc.Descendants("Tutorial")
                select new
                {
                    Author = tutorial.Element("Author").Value,
                    Title = tutorial.Element("Title").Value,
                    Date = tutorial.Element("Date").Value,
                };

foreach (var tutorial in tutorials)
{
    Console.WriteLine("author: " + tutorial.Author);
    Console.ReadKey();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:51:15+00:00Added an answer on May 26, 2026 at 8:51 pm

    Use the XElement to String conversion operator instead of the Value property:

    var tutorials = from tutorial in xmlDoc.Root.Elements("Tutorial")
                    select new
                    {
                        Author = (string)tutorial.Element("Author"),
                        Title = (string)tutorial.Element("Title"),
                        Date = (DateTime)tutorial.Element("Date"),
                    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets consider this xml file : <?xml version=1.0 encoding=UTF-8?> <root attribute=value> <element>myElement</element> </root> I'm
Consider this preferences.xml file: <?xml version=1.0 encoding=utf-8?> <PreferenceScreen xmlns:android=http://schemas.android.com/apk/res/android android:title=@string/preference_main> <PreferenceScreen android:title=@string/preference_sight android:key=category_sight> <ListPreference
Consider this simple xml element example: <parent foo=1 bar=2 foobar=3> <child/> </parent> In the
Consider this scenario: I've an XML file called person.xml with the following data in
Consider this example from w3schools : <?xml version=1.0 encoding=ISO-8859-1?> <bookstore> <book category=COOKING> <title lang=en>Everyday
Consider this xml: <parent> <child name=alpha /> </parent> and also this xml <parent> <child
Consider this simple XML document. The serialized XML shown here is the result of
consider this: I'm inside a (selfbuilt) XML Editor and am about to add a
I am parsing an XML file through Android Pull Parser technique. First, have a
Let's consider this sample example text file: Level: 1 Level: 1 Level: 2 Level:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.