Click here to Skip to main content
15,913,361 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have created an XML file, however 1 of the nodes is adding a namespace reference and I cant seem to remove it. Only adds to first element/node.

Code
XmlDocument doc =doc.Load(System.Configuration.ConfigurationManager.AppSettings["doctemploc"]);


XmlElement Blaett = doc.CreateElement("kk:Blaett","kk");
 XmlAttribute numbber = doc.CreateAttribute("number");

Blaett.Attributes.Append(numbber);


What I have tried:

nummer.RemoveAttribute("xmlns:kk");
nummer.Attributes().Where(a => a.IsNamespaceDeclaration).Remove()
nummer.Attributes("xmlns").Remove();
Posted
Updated 14-Jan-18 22:02pm

If you don't want the namespace in the output, then don't specify a namespace.

Replace:
XmlElement Blaett = doc.CreateElement("kk:Blaett","kk");
with:
XmlElement Blaett = doc.CreateElement("Blaett");

NB: Is it not valid for an XML document to use a namespace prefix without declaring the namespace.
 
Share this answer
 
Comments
Maciej Los 12-Jan-18 15:05pm    
I'd say: it's pretty obvious...
:laugh:
Ggalla17792 17-Jan-18 8:16am    
I tried this but it just moved the xmlns:kk="kk" down to the next element and repeated it again.

When this is removed by hand (from <Blaett numbber="311" xmlns:kk="kk"> to <Blaett numbber="311">) it validates against XSD
Richard Deeming 17-Jan-18 9:12am    
Because you're specifying the namespace on the next element (which you didn't show in your question).

If you don't want the namespace in the output, then don't specify the namespace when you create the elements!
Ggalla17792 18-Jan-18 3:08am    
Your good....I tried that also, removing the doc.CreateElement("kk:Blaett","kk") and subsequent ones.

It failed to validate against the XSD... it needs namespace a front ie <kk:Blaett.

Its been frustrating trying to get it working...
Richard Deeming 18-Jan-18 11:03am    
If it needs the namespace, then why are you trying to remove it?!

An XML file with a prefixed name that doesn't correspond to a namespace declaration is not valid.
Thanks for reply....I validated the file against it generated with above and it failed... its looking for prefixed with namespace....
 
Share this answer
 
Comments
Ggalla17792 15-Jan-18 4:49am    
This article here looks close to what I want...I just need to remove xmlns:kk="kk" from line <Blaett numbber="311" xmlns:kk="kk">

https://www.codeproject.com/Questions/1167775/Remove-namespace-from-XML
Richard Deeming 15-Jan-18 13:48pm    
If you want to reply to a solution, click the "Have a Question or Comment?" button under that solution.

DO NOT post your comment as a new "solution".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900