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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:37:12+00:00 2026-06-10T10:37:12+00:00

I’m writing an XML parser in C# to extract a lot (many files, coming

  • 0

I’m writing an XML parser in C# to extract a lot (many files, coming to over 200GB of data) of entries stored in xml form. I’m using Linq to XML to parse the files and get all the information into objects I created for them (14 objects; some objects contain lists of objects which contain lists of objects — It’s a lot of data).

The problem I’m running into is that some entries may have zero or more instances of a certain object. In a database context, this is no problem but I have no idea how to tell my program that if it can’t find the appropriate object under the tag that it should just create an empty/default one instead. Running the program gives me a “sequence contains no elements” error and exception handling doesn’t work since the program just exits the query when it finds an empty listing instead of ignoring it.

How do I go about handling zero-or-more cases in the linq-to-xml query?

the definitions for the objects in question are:

<!ELEMENT prior-registration-applications (other-related-in* , prior-registration-application*)>
<!ELEMENT other-related-in (#PCDATA)>
<!ELEMENT prior-registration-application (relationship-type? , number?)>

The code I’m getting the problem at is this:

priorRegistrationApplications = (
                        from q in e.Elements("prior-registration-applications")
                        select new PriorRegistrationApplications(
                            (string)q.Element("other-related-in"),
                            (from w in q.Elements("prior-registration-application")
                            select new PriorRegistrationApplication(
                            (string)w.Element("relationship-type"),
                            convertToInt((string)w.Element("number")))
                        ).ToList()
                        )
                        ).First(), //end prior-applications

The object definition for PriorRegistrationApplications is:

class PriorRegistrationApplications
{
    public string other_related_in; //only one appears, regardless of number of applications it seems.
    List<PriorRegistrationApplication> priorRegistrationApplications;


public PriorRegistrationApplications(string other_related_in, List<PriorRegistrationApplication> priorRegistrationApplications)
    {
        this.other_related_in = other_related_in;
        this.priorRegistrationApplications = priorRegistrationApplications;
    }

and for PriorRegistrationApplication:

class PriorRegistrationApplication
{
    public PriorRegistrationApplication() {
        relationship_type = "";
        number = 0;
    }

    public PriorRegistrationApplication(string relationship_type, int number)
    {
        this.relationship_type = relationship_type;
        this.number = number;
    }
  • 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-10T10:37:14+00:00Added an answer on June 10, 2026 at 10:37 am

    It’s not entirely clear where the code is failing (or why you have extraneous parentheses around your ToList call, or why you’re calling ToList at all) but you could consider simply changing your First() call to FirstOrDefault(). Then if that returns null, there were no elements. That’s not the same as creating a “default” one, but you could always use:

    foo = query.FirstOrDefault() ?? new PriorRegistrationApplication();
    

    if you really want. (Do you really have to have an empty object here?)

    Also note that this:

    convertToInt((string)w.Element("number"))
    

    not only violates naming conventions, but would probably be more simply written as:

    (int) w.Element("number")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from
I know there's a lot of other questions out there that deal with this

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.