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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:22:47+00:00 2026-06-10T03:22:47+00:00

This is my code: XElement itemsElement = new XElement(Items, string.Empty); //some code parentElement.Add(itemsElement); After

  • 0

This is my code:

XElement itemsElement = new XElement("Items", string.Empty);
//some code
parentElement.Add(itemsElement);

After that I got this:

<Items xmlns=""></Items>

Parent element hasn’t any namespace. What can I do, to get an Items element without the empty namespace attribute?

  • 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-06-10T03:22:49+00:00Added an answer on June 10, 2026 at 3:22 am

    It’s all about how you handle your namespaces. The code below creates child items with different namespaces:

    XNamespace defaultNs = "http://www.tempuri.org/default";
    XNamespace otherNs = "http://www.tempuri.org/other";
    
    var root = new XElement(defaultNs + "root");
    root.Add(new XAttribute(XNamespace.Xmlns + "otherNs", otherNs));
    
    var parent = new XElement(otherNs + "parent");
    root.Add(parent);
    
    var child1 = new XElement(otherNs + "child1");
    parent.Add(child1);
    
    var child2 = new XElement(defaultNs + "child2");
    parent.Add(child2);
    
    var child3 = new XElement("child3");
    parent.Add(child3);
    

    It will produce XML that looks like this:

    <root xmlns:otherNs="http://www.tempuri.org/other" xmlns="http://www.tempuri.org/default">
        <otherNs:parent>
            <otherNs:child1 />
            <child2 />
            <child3 xmlns="" />
        </otherNs:parent>
    </root>
    

    Look at the difference between child1, child2 and child3. child2 is created using the default namespace, which is probably what you want, while child3 is what you have now.

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

Sidebar

Related Questions

How to add an existing Xml string into a XElement? This code var doc
I have got this code: XDocument xdoc = XDocument.Load(URI); XElement root = xdoc.Element(forecast); //get
Getting this weird LINQ error. title = System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String Here is the code I have:
I have this code: XDocument xdoc = XDocument.Parse(xml); XElement xe = xdoc.Root.Element(program); string[] data
I have this code: XElement EcnAdminConf = new XElement(Type, new XElement(Connections, new XElement(Conn), //
I'm a rookie with LINQ to XML and I've got this code that works
This code is the core of a much larger script that works great in
This code fails on some machines: // Parse error: syntax error, unexpected '[' ...
How to adjust this code to work when RESPONSE is no more string but
I have this code that is supposed to load the attributes of an XML

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.