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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:18:27+00:00 2026-05-22T01:18:27+00:00

I have an XML file like: <myPrefix:Catalog xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml xmlns:sys=clr-namespace:System;assembly=mscorlib xmlns:myPrefix=clr-namespace:……..> <myPrefix:Item Name=Item1 Mode=All

  • 0

I have an XML file like:

<myPrefix:Catalog xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
 xmlns:sys="clr-namespace:System;assembly=mscorlib" 
 xmlns:myPrefix="clr-namespace:........">

  <myPrefix:Item Name="Item1" Mode="All" />
  <myPrefix:Item Name="Item2" Mode="Single" />

</myPrefix:Catalog>

With C# I create a new Item like:

XContainer container = XElement.Parse(xml);
XElement xmlTree = 
   new XElement("Item",
      new XAttribute("Name", item.Name),
      new XAttribute("Mode", item.Mode));

As you can see I don’t add the “myPrefix” prefix. Can anyone tell me how i can do that? I don’t want to declarde the xmlns again. Thanks, Peter

  • 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-22T01:18:28+00:00Added an answer on May 22, 2026 at 1:18 am

    Edit 1:
    If you add the namespace attribute aswell to the element this will force it to add the prefix. But you still end up with the xmlns attribute in the node. To remove it you’ll probably, as Jeff says, need to utilize XmlWriter.

    Edit 2:
    To get the EXACT XML you want you need to create the root element aswell:

    Edit 3:
    OK. I found a way to get what you want without XmlWriter:

    var xml = "<myPrefix:Catalog xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" xmlns:sys=\"clr-namespace:System;assembly=mscorlib\" xmlns:myPrefix=\"clr-namespace:........\"><myPrefix:Item Name=\"Item1\" Mode=\"All\" /></myPrefix:Catalog>";
    
    XNamespace presentation = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
    XNamespace xaml = "http://schemas.microsoft.com/winfx/2006/xaml";
    XNamespace mscorlib = "clr-namespace:System;assembly=mscorlib";
    XNamespace myPrefix = "clr-namespace:.......";
    
    XElement container = XElement.Parse(xml);
    
    var xmlTree = new XElement("Item",
                   new XAttribute("Name", "Item2"),
                   new XAttribute("Mode", "Single"));
    
    container.Add(xmlTree);
    
    foreach (var el in container.DescendantsAndSelf())
    {
      el.Name = myPrefix.GetName(el.Name.LocalName);
      var atList = el.Attributes().ToList();
      el.Attributes().Remove();
      foreach (var at in atList)
      {
        if (el.Name.LocalName == "Catalog" && at.Name.LocalName != "xmlns")
          continue;
        el.Add(new XAttribute(at.Name.LocalName, at.Value));
      }
    }
    
    container.Add(new XAttribute(XNamespace.Xmlns + "x", xaml));
    container.Add(new XAttribute(XNamespace.Xmlns + "sys", mscorlib));
    container.Add(new XAttribute(XNamespace.Xmlns + "myPrefix", myPrefix));
    

    Edit 4:
    Apparently there was an easier way… a LOT easier… see the other answers.

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

Sidebar

Related Questions

We have XML file like below... <?xml version='1.0'?> <T0020 xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://www.safersys.org/namespaces/T0020V1 T0020V1.xsd xmlns=http://www.safersys.org/namespaces/T0020V1> <IRP_ACCOUNT>
I have xml file like this <?xml version=1.0 encoding=UTF-8?> <specification xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:noNamespaceSchemaLocation=file://Desktop/normal.xsd> <university> <refstr>bdvl_te_skrm_stc</refstr>
Say I have an xml file like so: <ArrayOfInternetProxy xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:xsd=http://www.w3.org/2001/XMLSchema> <InternetProxy> <ProxyName />
I have an XML file that starts like this: <Elements name=Entities xmlns=XS-GenerationToolElements> I'll have
i have a xml file like this : https://gist.github.com/1966260 。 I want to dump
I have a XML file like this <a:books xmlns:a=ans> <a:book> <a:id> 1 </a:id> <a:title>The
I have an xml file like this: <users> <user name=user password=123 email=test@test.com/> </users> I
I have a XML File like that <?xml version=1.0 encoding=utf-8 ?> <Configurations> <EmailConfiguration> <userName>xxxx</userName>
I have an xml file like this: <root> <item> <name>one</name> <status>good</status> </item> <item> <name>two</name>
I have an xml file like this: <result> <customer> <id>1</id> <name>A</name> </customer> <customer> <id>2</id>

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.