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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:10:46+00:00 2026-05-25T02:10:46+00:00

I found some example of how to read the XML from an Excel spreadsheet

  • 0

I found some example of how to read the XML from an Excel spreadsheet >> HERE <<, and I am trying to get the code to work for me. I have to write a routine that takes the data from 4 of the 15 sheets in the workbook, and use that data to populate our database. Anyway….

This code is throwing an InvalidOperationException exception every time I attempt to query my Excel spreadsheet:

static XElement GetWorksheet(string sheetName, PackagePartCollection allParts) {
  XElement element = null;
  try {
    PackagePart worksheetPart = (from part in allParts
                                 where part.Uri.OriginalString.Equals(String.Format("/xl/worksheets/{0}.xml", sheetName))
                                 select part).Single();
    element = XElement.Load(XmlReader.Create(worksheetPart.GetStream()));
  } catch (Exception err) { // catches InvalidOperationException
    Console.WriteLine(err.Message);
  }
  return element;
}

The specific error is, “Sequence contains no elements”

I’m really not sure how to go about debugging it, because I’m not sure what the code is trying to do.

Could someone help me write it in Standard” C# before I try to implement the fancy LINQ expression? (that is LINQ, right?)

[Solved]

Using the snippet provided by JeffN825, I was able to create this:

static XElement GetWorksheet(string sheetName, PackagePartCollection allParts) {
  string xmlCompare = string.Format("/xl/worksheets/{0}.xml", sheetName);
  foreach (PackagePart part in allParts) {
    string original = part.Uri.OriginalString;
    if (original == xmlCompare) {
      XElement element = XElement.Load(XmlReader.Create(part.GetStream()));
      return element;
    }
  }
  return null;
}

It turns out that even though my sheet names in Excel are “HV”, “M2”, “CB” and “CC”, I still have to specify them as “sheet2”, “sheet3”, “sheet4” and “sheet5”.

I had to step through 161 PackagePart objects to see this.

LINQ is just so damn efficient that it can be hard to debug when there are problems.

  • 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-25T02:10:46+00:00Added an answer on May 25, 2026 at 2:10 am

    The .Single() is what’s throwing the exception. Either your query is returning no items or it’s returning > 1 item. If you’re not sure of the number of items you’re expecting, you should be using FirstOrDefault() which will return the first match or null if no matches are found.

    The non-LINQ equivalent is something like this

    PackagePart worksheetPart = null
    foreach(var part in allParts)
    {
       if (part.Uri.OriginalString.Equals(String.Format("/xl/worksheets/{0}.xml", sheetName))))
       {
           if (part != null) throw new InvalidOperationException(); // > 1 match
           worksheetPart = part;
       }
    }
    if (part == null) throw new InvalidOperationException(); // no match
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Found some old code, circa VS 2003. Now I have just VS 2008 (SP1)
At some places, I have read that stacks grow from a higher address to
Background: I'm working on some code to read in data from a file. Examples
I have a problem with the SQLite3 C API. I'm trying to read some
I've searched for an answer and found some c#-examples, but could not get this
I have done some research, and majority of the examples I have found use
I found some code in a project which looks like that : int main(int
I found some mapkit code on the internet that looks like this: - (void)recenterMap
I have found some in the Cappuccino website (vim, textmate and SubEthaEdit), but not
I have found some libraries or web services in PHP that does the job.

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.