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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:42:43+00:00 2026-06-17T06:42:43+00:00

I need to manipulate some xml files using Linq to xml. I have an

  • 0

I need to manipulate some xml files using Linq to xml.

I have an existing XDocument that I Load

Now I cannot seem to be able to add a namespace to it.

I do:

//Load an existing xml into a XDocument
XDocument xdoc=XDocument.Load(myXml);

//Create a namespace
 XNamespace myNS="http://www.w3.org/2001/XMLSchema-instance/MyShinyNewNamespace";
 xAttribute myAttr=new XAttribute(XNamespace.Xmlns +"myNS",myNS);

  //Add new namepsace to root

 xdoc.Root ????

What do you do here?

How do I retrieve my namespace?

How do I Remove/Replace?

many 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-06-17T06:42:44+00:00Added an answer on June 17, 2026 at 6:42 am

    First of all, while XML markup allows you to use

    <root xmlns="http://example.com/ns">
      <foo>
        <bar>baz</bar>
      </foo>
    </root>
    

    to use a single namespace declaration attribute to put the root element as well as those descendant elements into the declared namespace, when you manipulate the tree model you need to change the Name of all elements so you need e.g.

    XNamespace myNs = "http://example.com/ns";
    
    foreach (XElement el in xdoc.Descendants()) 
    {
      el.Name = myNs + el.Name.LocalName;
    }
    

    If you also want to set a certain prefix pf then addionally set

      xdoc.Root.Add(new XAttribute(XNamespace.Xmlns + "pf", myNs));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Python's minidom library to try and manipulate some XML files. Here is
I have some data that multiple Activities need to manipulate. Basically there is one
I'm using c++ to manipulate txt files. I need to write some numbers with
I need to manipulate an existing XML document, and create a new one from
I need to print XML that I manipulated using groovy and maintain the attribute
I'm using make in Windows and I need to manipulate some variables to create
I need a regex for my PHP script that will help me manipulate some
I have a .net web service that takes some xml data. From within the
I have some XML that needs to be manipulated into a string to render
I'm converting an existing program to C++ and here need to manipulate Sybase timestamps.

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.