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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:34:25+00:00 2026-05-12T05:34:25+00:00

While writing var e = element.NextSibling as HtmlElement; I get an invalid cast exception.

  • 0

While writing

var e = element.NextSibling as HtmlElement;

I get an invalid cast exception. I thought as prevents cast exception and returns null? because thats what i wanted/excepted. Either tell me how do i get e to be null instead of an exception? or why i got this exception when using the as keyword?

-edit- Correct it was element.NextSibling throwing the exception. How strange that its throwing a cast exception when i havent tried to cast it yet. I am not sure how to check if a next sibling exist. The error in NextSibling is below

    Message "Unable to cast COM object of type 'System.__ComObject' to interface type 'IHTMLElement'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{3050F1FF-98B5-11CF-BB82-00AA00BDCE0B}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."  string
  • 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-12T05:34:25+00:00Added an answer on May 12, 2026 at 5:34 am

    Are you sure it’s this particular statement that is causing the exception to be thrown?

    If you’ve wrapped it in a try/catch block to determine that it is, try picking it apart:

    var next = element.NextSibling;
    var e = next as HtmlElement;
    

    Perhaps the exception is thrown from within .NextSibling?

    In any case, it is not possible to override the as operator in C#, so it should always do what you expect it to do, and not throw that exception. I suspect the problem is elsewhere, but looks like it comes from this location.

    Edit: As suggested by @Jon Skeet in his deleted answer, I would also suggest you try to replicate the problem in a short, but complete, program, and post the program here in your question so that we can try to reproduce the problem ourselves. Also, trying to reproduce the problem in a shorter piece of code might give you valuable insight into the specific scenario you’re experiencing in your main project.

    Judging by the code, it looks like you’re parsing HTML code. If element is a HtmlElement object, then the .NextSibling method itself does not contain a cast that would produce that problem, but it does call another .NextSibling method through an interface reference, which means that Reflector alone won’t tell me which object is involved here.

    Edit in response to updated question: Unfortunately, I cannot help you with that part of the problem.

    It appears that the call to .NextSibling internally retrieves a COM object, which it believes should implement the IHTMLElement interface, but in fact it does not. This internal code, however, is using a hard cast, which thus fails with that exception.

    I don’t think you have any other choice than to wrap at the very least the call to .NextSibling in a try/catch block to avoid this, unless you can find some patently wrong with the HTML code it is attempting to parse that you could correct instead.

    Best of luck.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 221k
  • Answers 221k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer listView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> adapter, View view, int… May 13, 2026 at 12:05 am
  • Editorial Team
    Editorial Team added an answer There's a few factors to consider here. Spec coverage should… May 13, 2026 at 12:05 am
  • Editorial Team
    Editorial Team added an answer UPDATE: .NET is finally becoming an open-source, cross-platform solution. .NET… May 13, 2026 at 12:05 am

Related Questions

I've searched a good deal, and can't seem to find a satisfactory solution. I
Im just writing a small Ajax framework for re-usability in small projects and i've
I have a function, say: setValue: function(myValue) { ... } The caller might pass
I have a netbook with 1.20Ghz Processor & 1GB Ram. I'm running a C#

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.