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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:35:52+00:00 2026-05-16T08:35:52+00:00

In a project I am working on, I just finished writing an XSD for

  • 0

In a project I am working on, I just finished writing an XSD for my XML so other engineers could work on the XML easier. Along with this XSD came XML namespaces. In my current parsing code, I have been using something like this:

XElement element = xmlRoot.Element(XML.ELEMENT_NAME);

With XML being a class full of constants that is used throughout the program for XML input and output. But, now that namespaces are being used, this doesn’t work anymore. The obvious, but barely elegant, solution is to go in and make all of the parsing code look like this:

XElement element = xmlRoot.Element(XML.NAMESPACE + XML.ELEMENT_NAME);

Thinking about it more, it would look a lot better to go off and define an extension method to add in this namespace and return the XElement. This idea would get the code looking something like this:

XElement element = xmlRoot.GetMyElement(XML.ELEMENT_NAME);

These ideas are what I have come up with so far to deal with the namespace issue.
Basically, my question here is this:
Is there a better method for parsing XElements with a known, constant namespace? If so, what is it?

  • 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-16T08:35:52+00:00Added an answer on May 16, 2026 at 8:35 am

    Well, no one’s provided an alternative, and I’ve already implemented this in the code. So, I’m going to go ahead and put this in as an answer so I can close this out.

    I went ahead and used the extension method that automatically added in the namespace. Note that this only works if you have a constant namespace! Otherwise, you may want to add another argument to your extension method simply to avoid doing the weird namespace concatenation.

          public static XElement
          GetNamespaceElement(this XElement element, string ns, string name)
          {
              XNamespace n = new XNamespace();
              n.NamespaceName = ns;
              return element.Element(n + name);
          }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just finished writing the core section of a project I am working
So I just finished working the project at http://msdn.microsoft.com/en-us/data/gg685489 . I am trying to
I've just finished working for a client which has burnt millions on a project
I'm working with .Net using VB and have just finished a website project and
Working on a project, nearly finished and just tidying up the HTML and I
My project was working just fine until this morning. I was using xcode 4.3,
I just finished building my new COM project (C#, .NET 3.5). This project will
So I'm working on just a learning project to expose myself to doing some
I have just begun working on a project which uses Mercurial as a version
I've just started working on an existing CakePHP project. I will be developing a

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.