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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:02:29+00:00 2026-06-09T13:02:29+00:00

I have the following xml, from which I am trying to remove all xmlns

  • 0

I have the following xml, from which I am trying to remove all “xmlns” attributes using LINQ to XML:

    <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
      <StackPanel>
        <LinearLayout xmlns="clr-namespace:AndroidAssembly;assembly=AndroidAssembly"/>                
        <TextView xmlns="clr-namespace:AndroidAssembly;assembly=AndroidAssembly">

  </StackPanel>
</Window>

The following code is used. It hits “attributesToRemove.Remove()” for each “xmlns” attribute in the document. But when I save the document, I still have the original XML. Any idea, what is the problem might be?

 var sr = new StringReader(richTextBoxOriginalXml.Text);
            XDocument xdoc = XDocument.Load(sr);            
            foreach(var node in xdoc.Descendants().ToList())
            {
                var xmlns = node.Attributes().FirstOrDefault(a => a.Name == "xmlns");
                if (xmlns !=null)
                {
                    var attributesToRemove = node.Attributes("xmlns").ToList();
                    attributesToRemove.Remove();
                }
            }

            var writer = new StringWriter();
            var xmlWriter = new XmlTextWriter(writer);
            xmlWriter.Formatting = Formatting.Indented;
            xdoc.WriteTo(xmlWriter);
            richTextBoxTransformed.Text = writer.GetStringBuilder().ToString();
  • 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-09T13:02:31+00:00Added an answer on June 9, 2026 at 1:02 pm

    Like Marc Gravell pointed out before I posted, xmlns attribute (unqualified) is not an attribute as such, it’s considered part of the name of the element.

    <StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
        <LinearLayout xmlns="clr-namespace:AndroidAssembly;assembly=AndroidAssembly" />
        <TextView xmlns="clr-namespace:AndroidAssembly;assembly=AndroidAssembly" />
    </StackPanel>   
    

    StackPanel tag has no attributes here. It’s name, however, is {http://schemas.microsoft.com/winfx/2006/xaml/presentation}StackPanel.

    Name.Namespace is {http://schemas.microsoft.com/winfx/2006/xaml/presentation} (XNamespace type) and Name.LocalName == "StackPanel".

    Thus you need to effectively rename the element. It should work.

    Oversimplified (but compilable) program:

    using System;
    using System.Linq;
    using System.Xml.Linq;
    
    namespace ConsoleApplication
    {
        class Program
        {          
            static void Main(string[] args)
            {
                var raw = @"<Window xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
                xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
          <StackPanel>
            <LinearLayout xmlns=""clr-namespace:AndroidAssembly;assembly=AndroidAssembly""/>                
            <TextView xmlns=""clr-namespace:AndroidAssembly;assembly=AndroidAssembly""/>    
      </StackPanel>
    </Window>";
    
                var xml = XElement.Parse(raw);
                var descendants = xml.Descendants().ToArray();
                var stackPanel = descendants.First();
                Console.WriteLine(stackPanel.ToString());
    
                Console.WriteLine("==================================");
                stackPanel.Name = stackPanel.Name.LocalName; // stripping the namespace off
                Console.WriteLine(stackPanel.ToString());
    
                Console.ReadLine();
            }
        }
    }
    

    Output:

    <StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
      <LinearLayout xmlns="clr-namespace:AndroidAssembly;assembly=AndroidAssembly" />
      <TextView xmlns="clr-namespace:AndroidAssembly;assembly=AndroidAssembly" />
    </StackPanel>
    ==================================
    <StackPanel>
      <LinearLayout xmlns="clr-namespace:AndroidAssembly;assembly=AndroidAssembly" />
      <TextView xmlns="clr-namespace:AndroidAssembly;assembly=AndroidAssembly" />
    </StackPanel>
    

    The difference in the StackPanel element is easy to notice…

    By the way, there is a typo in the XML you posted (TextView tag is not closed), but I can’t edit it out – edits must be at least 6 characters long 🙂 (Why 6?)

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

Sidebar

Related Questions

I have the following XML from which I am trying to generate HTML files.
I have the following XML LINQ query from my XDocument. var totals = (from
I have following XML. I was able to remove all namespaces but not able
I have a windows service which is trying to access an xml file from
Possible Duplicate: Use LINQ to read all nodes from XML I am trying to
I am trying to read tags from XML using LibXML. I can print all
I'm trying to parse xml from SharePoint service (lists) using jquery. I have XMLHttpRequest
I have following object structure, deseralized from XML (WS): <ns2:Category> <ns2:CategoryId>800003</ns2:CategoryId> <ns2:CategoryName>Name1</ns2:CategoryName> <ns2:Categories> <ns2:Category>
I have to read the xml node name from the following XML, but I
I have following code for loading image from url in xml parsing endElement method

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.