You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Basically if we implicitly declare the default namespace of the XElement, we cannot access it through the GetDefaultNamespace() function, which returns XNamespace.None, which is incorrect, since the namespace has already been defined in the current scope and there is no way to change the namespace without having to recursively clone the entire XML tree...
Consider the code below:
using System;using System.Xml.Linq;varel=new XElement("{urn:sharplab:io}root");
Console.WriteLine(" ------------ Construct ------------");// should not be empty. namespace is declared in this scope...
Console.WriteLine("default namespace: "+ el.GetDefaultNamespace());// fine
Console.WriteLine("namespace: "+ el.Name.Namespace);
Console.WriteLine("xml: "+el);el= XElement.Parse(el.ToString());
Console.WriteLine("\n ------------ Parse ------------");// not empty now, correct behaviour. because xmlns is declared in this scope but now its just add XAttribute to it.
Console.WriteLine("default namespace: "+ el.GetDefaultNamespace());// fine
Console.WriteLine("namespace: "+ el.Name.Namespace);
Console.WriteLine("xml: "+el);
As you can see, when creating the XElement, it does not consider the problem with the XElement as a valid value for GetDefaultNamespace, which is that it considers ONLY what is linked to the XName and it does not make sense.
Since what defines the namespace is the URI that qualifies it, if it has a prefix, this prefix determines the scope of the URI. So what should say which namespace the XElement has is the xmlns or xmlns:prefix attribute and not the XName.Namespace property.
I've even opened an issue with this problem before, but since almost a month have passed without any response from anyone, I ended up choosing to close it. But I had to do some projects that use and need XML without having to rewrite the entire DOM mechanism, I encountered this same problem with Namespaces again.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Basically if we implicitly declare the default namespace of the XElement, we cannot access it through the
GetDefaultNamespace()
function, which returnsXNamespace.None
, which is incorrect, since the namespace has already been defined in the current scope and there is no way to change the namespace without having to recursively clone the entire XML tree...Consider the code below:
SharpLab
As you can see, when creating the XElement, it does not consider the problem with the XElement as a valid value for
GetDefaultNamespace
, which is that it considers ONLY what is linked to the XName and it does not make sense.Since what defines the namespace is the URI that qualifies it, if it has a prefix, this prefix determines the scope of the URI. So what should say which namespace the XElement has is the
xmlns
orxmlns:prefix
attribute and not theXName.Namespace
property.I've even opened an issue with this problem before, but since almost a month have passed without any response from anyone, I ended up choosing to close it. But I had to do some projects that use and need XML without having to rewrite the entire DOM mechanism, I encountered this same problem with Namespaces again.
Beta Was this translation helpful? Give feedback.
All reactions