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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:41:22+00:00 2026-05-13T21:41:22+00:00

If a node belongs to a namespace, it’s children by default belong to the

  • 0

If a node belongs to a namespace, it’s children by default belong to the same namespace. So there’s no need to provide an xmlns attribute on each child, which is good.

However.

If I create two nodes like this:

Dim parent = <parent xmlns="http://my.namespace.org"/>
Dim child = <child xmlns="http://my.namespace.org">value</child>

parent.Add(child)

Console.WriteLine(parent.ToString)

The result is this:

<parent xmlns="http://my.namespace.org">
  <child xmlns="http://my.namespace.org">value</child>
</parent>

But, if create them in a less convenient way:

Dim parent = <parent xmlns="http://my.namespace.org"/>
Dim child As New XElement(XName.Get("child", "http://my.namespace.org")) With {.Value = "value"}

parent.Add(child)

Console.WriteLine(parent.ToString)

The result is more desirable:

<parent xmlns="http://my.namespace.org">
  <child>value</child>
</parent>

Obviously, I’d prefer to use the first way because it is so much more intuitive and easy to code. There’s also another reason to not use method 2 — sometimes I need to create nodes with XElement.Parse, parsing a string that contains an xmlns attribute, which produces exactly same results as method 1.

So the question is — how do I get the pretty output of method 2, creating nodes as in method 1? The only option I see is to create a method that would clone given XElement, effectively recreating it according to method 2 pattern, but that seems ugly. I’m looking for a more obvious solution I overlooked for some reason.

  • 1 1 Answer
  • 1 View
  • 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-13T21:41:23+00:00Added an answer on May 13, 2026 at 9:41 pm

    Ugh…namespaces – they will be the death of me.

    Here you go:

    Dim ns As XNamespace = "http://my.namespace.org"
    Dim parent = <<%= ns + "parent" %>/>
    Dim child = <<%= ns + "child" %>>value</>
    parent.Add(child)
    

    To be able to use XElement.Parse and keep child nodes in sync with their parent nodes’ namespaces, it’s best to use global namespaces. Really easy to do in VB.NET. At the top of your module/class, just use an Imports and all parents and children will use this namespace. For example:

    Imports <xmlns="http://my.namespace.org">
    Module Main
        Sub SomeSub()
            Dim child = <child>value</child>
            Dim parent = <parent><%= child %></parent>
            Console.WriteLine(parent.ToString)
        End Sub
    End Module
    

    Note that the <child/> element is created first. The same would apply to a non-default namespace, like Imports <xmlns:p="http://parent.namespace.org"> and then creating with <p:child/> and <p:parent/>.

    I once read, but have yet to find again, that mixing XML Literals with I-don’t-know-what-you-call-it-but-it’s-that-parent.Add(something)-thing is a bad idea.

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

Sidebar

Related Questions

Given a DAG, in which each node belongs to a category, how can this
I need create a tree structure recursively. In the tree each node has different
4 node Binary Search tree: 2143. there are 3 ways to insert and get
How to get list of terms of a node ( by node id) belongs
I have two content types, book and chapter. Each chapter node contains a node
In my database I store a number of topics and examples. Each example belongs
I mean, how can I find which of two elements belongs to a node
Is there a way to show subnode if the parent node is hidden in
Node.js: Should package.json dependencies be managed manually only or there is some tools that
I need to execute MapReduce on my Cassandra cluster, including data locality, ie. each

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.