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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:54:40+00:00 2026-05-24T00:54:40+00:00

I have a use-case where I’m required to read in some information from an

  • 0

I have a use-case where I’m required to read in some information from an XML file and act on it accordingly. The problem is, this XML file is technically allowed to be empty or full of whitespace and this means “there’s no info, do nothing”, any other error should fail hard.

I’m currently thinking about something along the lines of:

    public void Load (string fileName)
    {
        XElement xml;
        try {
            xml = XElement.Load (fileName);
        }
        catch (XmlException e) {
            // Check if the file contains only whitespace here
            // if not, re-throw the exception
        }
        if (xml != null) {
            // Do this only if there wasn't an exception
            doStuff (xml);
        }
        // Run this irrespective if there was any xml or not
        tidyUp ();
    }

Does this pattern seem ok? If so, how do people recommend implementing the check for if the file contained only whitespace inside the catch block? Google only throws up checks for if a string is whitespace…

Cheers muchly,

Graham

  • 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-24T00:54:42+00:00Added an answer on May 24, 2026 at 12:54 am

    Well, the easiest way is probably to make sure it isn’t whitespace in the first place, by reading the entire file into a string first (I’m assuming it isn’t too huge):

    public void Load (string fileName)
    {
        var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
        var reader = new StreamReader(stream, Encoding.UTF8, true);
        var xmlString = reader.ReadToEnd();
    
        if (!string.IsNullOrWhiteSpace(xmlString)) {  // Use (xmlString.Trim().Length == 0) for .NET < 4
            var xml = XElement.Parse(xmlString);    // Exceptions will bubble up
            doStuff(xml);
        }
    
        tidyUp();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this use case of an xml file with input like Input: <abc
In my app i have one use case Download Folder(s) from web server this
I have the following use case, my file contents look like this a b
I have a use case with NServiceBus explained in this question. Essentially there is
In C# I have a use case where I have a mapping from int
I have this use case scenario: there are places which are either playgrounds, restaurants,
I have a use case where I need to add information about the user
I have a use case , where I need to display some log files
I have this use case for which I am generating an image in the
I have this use case that is very similar to the robot-legs example of

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.