Ehab_Hosny

Hi Guys,

I am facing a problem in XML that i have an application which reads an XML file (.Config) file and edit it the problem that in some cases when i add or edit certain elements or nodes it adds the attribute xmlns ="" and it causes some problems to my application so i want to know how to avoid or remove this attribute from my file

Wish to have response asap

Regards,

Ehab Hosny




Re: XML and the .NET Framework Unneeded xmlns Attribute

Nikolay Glushnev

Could you post an example of the XML data Which .NET classes you use to process the file

In some cases, the default namespace declaration xmlns ="" should not be removed because its removal will change the meaning of the document. For example:
Code Snippet
<a xmlns="urn:test">
<b xmlns=""/> <!-- This default namespace declaration should not removed here -->
</a>







Re: XML and the .NET Framework Unneeded xmlns Attribute

Ehab_Hosny

In fact what im doing is editing in a .config file so im adding some elements or nodes to edit it in the file

and im using .net classes like XMLDocument, XMLNode, XMLElement, ..etc

for example im adding something like that

<add key="MyKey" value="MyValue"/>

or

<ConnectionData id= "1" name="ConStr" />

and after saving file i found it like that

<add key="MyKey" value="MyValue" xmlns="" />

and it affects the application that is using this .config file. i tried to remove the attributr xmlns from the file after saving it using .net xml classes but it couldn't as it wasn't one of the attributes of the node although it is exist.




Re: XML and the .NET Framework Unneeded xmlns Attribute

Martin Honnen

That "problem" usually occurs because the required xmlDocument.CreateElement overload taking a namespace URI is not used. The namespace of an element node is determined when you create it thus if you want to create an add element in a certain namespace (e.g. http://example.com/2007/ns1) then you need to pass that namespace to an overload of CreateElement that takes a namespace URI e.g.

XmlElement addElement = xmlDocumentInstance.CreateElement("add", "http://example.com/2007/ns1");

That way you create an add element in that particular namespace.

If you simply do

XmlElement addElement = xmlDocumentInstance.CreateElement("add");

then you create an add element in no namespace. Consequently when you later insert that element to a parent node that is in a namespace and then serialize the changed document the serializer adds xmlns="" as the add element is in no namespace.

So decide which namespace your elements are supposed to have and pass that namespace URI to CreateElement each time you create an element, then the problem of xmlns="" goes away. For instance assuming you know the new parent node of your element to append to you and you want the new element to have the same namespace then you can simply do e.g.

Code Snippet

XmlElement addElement = xmlDocumentInstance.CreateElement("add", newParentNode.NamespaceURI);

addElement.SetAttribute("key", "MyKey");

// .. set further attributes as needed

newParentNode.AppendChild(addElement);






Re: XML and the .NET Framework Unneeded xmlns Attribute

Ehab_Hosny

this may be usefule if im just adding elements but i want somthing with any node as im usning the function XMLNode.AppendChild(); and it encounters the same problem too. so what shall i do with it






Re: XML and the .NET Framework Unneeded xmlns Attribute

Martin Honnen

Please show us a sample of the exact code you are using, then we can make suggestions on how to change that.






Re: XML and the .NET Framework Unneeded xmlns Attribute

Ehab_Hosny

ok, the previous answer succeeded when adding new element but now i need to add new node

for example:

XMLNode MyNode = MyXMLDoc.Node[0];

MyParentNode.AppendChild(MyNode);

OR

MyParentNode.ReplaceChild(MyNode,OldNode);






Re: XML and the .NET Framework Unneeded xmlns Attribute

Martin Honnen

Sorry, MyXMLDoc.Node[0] does not make any sense to me, an XmlDocument instance does not have a property named Node.






Re: XML and the .NET Framework Unneeded xmlns Attribute

Ehab_Hosny

I meant that


for example:

XMLNode MyNode = MyXMLDoc.ChildNodes[0];

MyParentNode.AppendChild(MyNode);

OR

MyParentNode.ReplaceChild(MyNode,OldNode)


Not That


for example:

XMLNode MyNode = MyXMLDoc.Nodes[0];

MyParentNode.AppendChild(MyNode);

OR

MyParentNode.ReplaceChild(MyNode,OldNode)






Re: XML and the .NET Framework Unneeded xmlns Attribute

Martin Honnen

A node gets its namespaceURI when it is created and keeps it for the rest of its "life". I can only guess that you are moving an element to a different parent node that has a different namespace and expect the namespace to be changed because of the new parent. That however does not happen, the namespaceURI of a node cannot be changed. Thus if you want to change the namespace of an element node then you need to create a new element node with the same local name and the new namespace. If the element has child elements then you need to do the same for the child element and the descendants too. An XSLT stylesheet can do such a job easily, if you use the DOM API then you need to write a recursive function traversing the descendants to do the job.






Re: XML and the .NET Framework Unneeded xmlns Attribute

Ehab_Hosny

the problem that i currently has is that it creates the xmlns attribute in the new added node and what i need is to ignore it not changing it. it appears like that xmlns="" so it has no value and appears whenever i create any node and it appears only when the edited file has xmlns attribute in its root.





Re: XML and the .NET Framework Unneeded xmlns Attribute

Martin Honnen

I have tried to point out two times that the namespace of a node is determined when you create it, not when you insert it. So when you create your new nodes you need to make sure you create them in the proper namespace. If you already have a node and that is in no namespace and you insert it somewhere into a node that is in a namespace then the serializer has to add the xmlns="" to ensure the node remains in no namespace. That is how the DOM works, there is no way to change that. So as already suggested, make sure you create nodes in the namespace you want them in.






Re: XML and the .NET Framework Unneeded xmlns Attribute

Ehab_Hosny

Ok i got that and many thanx for ur cooperation and fast respons