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 8037979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:05:02+00:00 2026-06-05T03:05:02+00:00

Here is my problem: I have this XML file: <?xml version=1.0 encoding=utf-8 ?> <settings>

  • 0

Here is my problem:

I have this XML file:

<?xml version="1.0" encoding="utf-8" ?>
<settings>
  <app name="Application1">
    <log name="Log1" path="d:\paths\" filename="Log1File"/>
    <log name="Log2" path="d:\paths\"/>
    <log name="log3" path="d:\paths\" filename="Log3File"/>
  </app>
</settings>

And I’m trying to read it with LINQ and create object of this class:

  public class Apps
  {
    public string Name { get; set; }
    public IEnumerable<Logs> Logs { get; set; }
  }

  public class Logs
  {
    public string Name { get; set; }
    public string Path { get; set; }
    public string Filename { get; set; }
  }

So far I managed to create this bit of code however looks like it only gets first log element mean time I need all log elements for each app element:

   public static IEnumerable<Apps> GetAllApps()
    {
      var items = from a in db.Descendants("app")
                  orderby a.Attribute("name").Value
                  select new Apps
                  {
                    Name = a.Attribute("name").Value,
                    Logs = from b in a.Descendants("log")
                           select new Logs 
                           { 
                            Name = b.Attribute("name").Value,
                            Path = b.Attribute("path").Value,
                            Filename = b.Attribute("filename").Value
                           }
                  };

      return items;
    }
  • 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-06-05T03:05:04+00:00Added an answer on June 5, 2026 at 3:05 am

    I used fluent API, but let you adapt as you prefer…

    Problem is a NullReferenceException, as one of your Logs in xml has no “filename” attribute. And when you use “Value” on a null, you get a NRE.

    So, check if the Attribute exists before trying to get it’s value.

    var it = db.Descendants("app")
                        .OrderBy(app => app.Attribute("name").Value)
                        .Select(app => new Apps() {
                            Name = app.Attribute("name").Value,
                            Logs = app.Descendants("log").Select(a =>
                                new Logs() {
                                    Name = a.Attribute("name") != null ? a.Attribute("name").Value : null,
                                    Path = a.Attribute("path") != null ? a.Attribute("path").Value : null,
                                    Filename = a.Attribute("filename") != null ? a.Attribute("filename").Value : null
                                }).ToList()
                        }).ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I have an XML file that looks like this: <?xml version=1.0 encoding=UTF-8?> <data>
I have this layout file in my android application: <?xml version=1.0 encoding=utf-8?> <TabHost xmlns:android=http://schemas.android.com/apk/res/android
I have a xml file which has the following header: <?xml version=1.1 encoding=UTF-8?> .
The basic problem I have here is that I have one xml file that
Very frustrating problem I have here. I have this code: Button b = findViewById(android.R.id.button1);
I have this problem that drives me mad, so I am here to ask
I have a problem here with this code. I'm opening a socket, then listening
Hy! Here is my problem: i have a profile and this profile has for
i have a small problem here which i cannot fix,This post goes through but
So, here's my problem. I have objects like this public class FooClass{ private int

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.