XElement only adds a prefix

I have an XML file, for example:

<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 element 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 am not adding the prefix "myPrefix". Can someone tell me how can I do this? I do not want to declare xmlns again. Thanks Peter

+7
source share
3 answers

Change 1:
If you add a namespace attribute to an element, this will force it to add a prefix. But you still get the xmlns attribute in node. To remove it, you probably need to use XmlWriter, as Jeff says.

Edit 2:
To get the EXACT XML you need, you also need to create a root element:

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)); 

Change 4:
Apparently there was an easier way ... LOTION is easier ... see Other answers.

+3
source
  XElement container = XElement.Parse(xml); XNamespace myPrefix = container.GetNamespaceOfPrefix("myPrefix"); XElement xmlTree = new XElement(myPrefix + "Item", new XAttribute("Name", item.Name), new XAttribute("Mode", item.Mode)); container.Add(xmlTree); 
+7
source

You need to build any new elements in the namespace. Assuming you know the namespace prefix that you want in the XML sample, do this as follows:

  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>"; XElement catalog = XElement.Parse(xml); XNamespace myP = catalog.GetNamespaceOfPrefix("myPrefix"); catalog.Add(new XElement(myP + "Item", new XAttribute("Name", "foo"), new XAttribute("Mode", "bar"))); 
+2
source

All Articles