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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:57:29+00:00 2026-05-14T06:57:29+00:00

I have an XML file like this: <SiteConfig> <Sites> <Site Identifier=a /> <Site Identifier=b

  • 0

I have an XML file like this:

<SiteConfig>
  <Sites>
    <Site Identifier="a" />
    <Site Identifier="b" />
    <Site Identifier="c" />
  </Sites>
</SiteConfig>

The file is user-editable, so I want to provide reasonable error message in case I can’t properly parse it. I could probably write a .xsd for it, but that seems kind of overkill for a simple file.

So anyway, when querying for the list of <Site> nodes, there’s a couple of ways I could do it:

var doc = XDocument.Load(...);

var siteNodes = from siteNode in 
                  doc.Element("SiteConfig").Element("Sites").Elements("Site")
                select siteNode;

But the problem with this is that if the user has not included the <SiteUrls> node (say) it’ll just throw a NullReferenceException which doesn’t really say much to the user about what actually went wrong.

Another possibility is just to use Elements() everywhere instead of Element(), but that doesn’t always work out when coupled with calls to Attribute(), for example, in the following situation:

var siteNodes = from siteNode in 
                  doc.Elements("SiteConfig")
                     .Elements("Sites")
                     .Elements("Site")
                where siteNode.Attribute("Identifier").Value == "a"
                select siteNode;

(That is, there’s no equivalent to Attributes("xxx").Value)

Is there something built-in to the framework to handle this situation a little better? What I would prefer is a version of Element() (and of Attribute() while we’re at it) that throws a descriptive exception (e.g. “Looking for element <xyz> under <abc> but no such element was found”) instead of returning null.

I could write my own version of Element() and Attribute() but it just seems to me like this is such a common scenario that I must be missing something…

  • 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-14T06:57:30+00:00Added an answer on May 14, 2026 at 6:57 am

    You could implement your desired functionality as an extension method:

    public static class XElementExtension
    {
        public static XElement ElementOrThrow(this XElement container, XName name)
        {
            XElement result = container.Element(name);
            if (result == null)
            {
                throw new InvalidDataException(string.Format(
                    "{0} does not contain an element {1}",
                    container.Name,
                    name));
            }
            return result;
        }
    }
    

    You would need something similar for XDocument. Then use it like this:

    var siteNodes = from siteNode in 
        doc.ElementOrThrow("SiteConfig")
           .ElementOrThrow("SiteUrls")
           .Elements("Sites")
        select siteNode;
    

    Then you will get an exception like this:

    SiteConfig does not contain an element SiteUrls
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a xml file like this : https://gist.github.com/1966260 。 I want to dump
I have a XML file like this and I just want to get the
I have an xml file like this: <users> <user name=user password=123 email=test@test.com/> </users> I
I have a XML file like this : <?xml version=1.0 encoding=UTF-8 standalone=no ?> <user>
I have a XML file like this <?xml version=1.0 encoding=utf-8?> <Survey Name=Aerosmith's Survey Id=S2
I have an xml file like this: <result> <customer> <id>1</id> <name>A</name> </customer> <customer> <id>2</id>
I have created an xml file like this: <?xml version=1.0 encoding=utf-8?> <ListView xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=match_parent
I have an XML file looks like this: <?xml version=1.0 encoding=utf-8 ?> <PathMasks> <Mask
I have an XML file that reads like this <abc> <ab>value</ab> <aa>time</aa> <ac>money</ac> </abc>
I have an options_menu.xml file like this: <?xml version=1.0 encoding=utf-8?> <menu xmlns:android=http://schemas.android.com/apk/res/android> <item android:id=@+id/search

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.