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

  • Home
  • SEARCH
  • 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 3356760
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:33:06+00:00 2026-05-18T02:33:06+00:00

I’m having some issues with namespaces in LINQ. So going off of MSDN’s Tutorial

  • 0

I’m having some issues with namespaces in LINQ. So going off of MSDN’s Tutorial we can do the following to add namespaces to an XML document with LINQ:

// Create an XML tree in a namespace.
XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "Root",
    new XElement(aw + "Child", "child content")
);
Console.WriteLine(root);

Which will give you the XML:

<Root xmlns="http://www.adventure-works.com">
  <Child>child content</Child>
</Root>

But what I want to do is this:

XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "Roots");
XElement child = new XElement("Child", "child content");
root.Add(child);

Console.WriteLine(root);

But this gives me the following xml:

<Root xmlns="http://www.adventure-works.com">
    <Child xmlns="">child content</Child>
</Root>

The problem is I don’t want the xmlns="" in my child elements (EDIT: I don’t want any namespaces in my child elements), I want it to look just like the first way produces it. I am building a fairly large XML file so I can’t add all the elements in the same declaration and need to be able to use the Add and SetAttributeValue methods of multiple XElements still having an xmlns on the first element. Any ideas how I can accomplish this?

Thanks!

  • 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-18T02:33:06+00:00Added an answer on May 18, 2026 at 2:33 am

    This behavior is fundamental to the XML information set. The effect of the xmlns declaration is scoped to all child content; since you did not specify a namespace for the child element, the standard behavior is to place it in the unnamed namespace. In order to nest an element in an unnamed namespace inside of one that declares a default namespace, the xmlns='' declaration is required to reset the default namespace to unnamed.

    To summarize, based on your edit:

    • If you don’t want any namespaces in your child elements, then an explicit xmlns='' is required by the XML Namespaces specification in order to override the namespace declared in the document element.
    • If you don’t want any namespace declarations (i.e. xmlns='' stuff) in your child elements, you need to make sure the same namespace is added to the names of all of them in code.

    There is no way I know of to make an XElement automatically inherit the namespace of its parent. After you are done building the DOM, though, you can always postprocess it to set one namespace everywhere with something similar to this:

    foreach(var element in doc.Descendants())
        element.Name = aw + element.Name.LocalName;
    

    I don’t believe doing this is advisable for anything other than throwaway code, though, since it will likely have untoward effects on maintainability. A number of XML documents I’ve had to deal with mix namespaces, and “smashing” namespaces like the above code snippet does would clobber the semantics of a mixed-namespace document.

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

Sidebar

Related Questions

No related questions found

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.