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
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:
Edit 4:
Apparently there was an easier way… a LOT easier… see the other answers.