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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:39:08+00:00 2026-05-10T16:39:08+00:00

I have a project where I am taking some particularly ugly live HTML and

  • 0

I have a project where I am taking some particularly ugly ‘live’ HTML and forcing it into a formal XML DOM with the HTML Agility Pack. What I would like to be able to do is then query over this with Linq to XML so that I can scrape out the bits I need. I’m using the method described here to parse the HtmlDocument into an XDocument, but when trying to query over this I’m not sure how to handle namespaces. In one particular document the original HTML was actually poorly formatted XHTML with the following tag:

<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'> 

When trying to query from this document it seems that the namespace attribute is preventing me from doing something like:

var x = xDoc.Descendants('div'); // returns null 

Apparently for those ‘div’ tags only the LocalName is ‘div’, but the proper tag name is the namespace plus ‘div’. I have tried to do some research on the issue of XML namespaces and it seems that I can bypass the namespace by querying this way:

var x =      (from x in xDoc.Descendants()      where x.Name.LocalName == 'div'      select x); // works 

However, this seems like a rather hacky solution and does not properly address the namespace issue. As I understand it a proper XML document can contain multiple namespaces and therefore the proper way to handle it should be to parse out the namespaces I’m querying under. Has anyone else ever had to do this? Am I just making it way to complicated? I know that I could avoid all this by just sticking with HtmlDocument and querying with XPath, but I would rather stick to what I know (Linq) if possible and I would also prefer to know that I am not setting myself up for further namespace-related issues down the road.

What is the proper way to deal with namespaces in this situation?

  • 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. 2026-05-10T16:39:09+00:00Added an answer on May 10, 2026 at 4:39 pm

    Using LocalName should be okay. I wouldn’t consider it a hack at all if you don’t care what namespace it’s in.

    If you know the namespace you want and you want to specify it, you can:

    var ns = '{http://www.w3.org/1999/xhtml}'; var x  = xDoc.Root.Descendants(ns + 'div'); 

    (MSDN reference)

    You can also get a list of all the namespaces used in the document:

    var namespaces = (from x in xDoc.Root.DescendantsAndSelf()                   select x.Name.Namespace).Distinct(); 

    I suppose you could use that to do this but it’s not really any less of a hack:

    var x = namespaces.SelectMany(ns=>xDoc.Root.Descendants(ns+'div')); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yort has post on how to do this on his… May 11, 2026 at 7:27 pm
  • Editorial Team
    Editorial Team added an answer At the program memory locations immediately after $0000 interrupt vectors… May 11, 2026 at 7:27 pm
  • Editorial Team
    Editorial Team added an answer How about SetTimer. Create a wrapper function to use as… May 11, 2026 at 7:27 pm

Related Questions

I've got a junk directory where I toss downloads, one-off projects, email drafts, and
My program that I am writing's purpose arose with this issue: There are two
I mainly develop in native C++ on Windows using Visual Studio. A lot of
I work in a medium sized team and I run into these painfully large

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.