EDN Admin
Well-known member
What do you think of this code :
<pre lang="x-c# public class Sample
{
public static void Main()
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<root xmlns=urn:root>" +
"<child xmlns=urn:child/>" +
"</root>");
var child = doc.FirstChild.FirstChild as XmlElement;
string ns = "urn:root";
string prefix = child.GetPrefixOfNamespace(ns);
string nsFromPrefix = child.GetNamespaceOfPrefix(prefix);
Debug.Assert(ns == nsFromPrefix);
}
}[/code]
<br/>
From my point of view, there is a bug on the child.GetPrefixOfNamespace(ns) since in this case the prefix should not be empty when query on node child ? Maybe our XML is bad but I didnt find anything about it.
Our scenario was to add an attribute on the child node but within the root namespace. The child node was added by some function and can be in a different namespace and we were looking how to add the node for pretty printing, with something like :
<pre lang="x-c# if(string.IsNullOrEmpty(child.GetPrefixOfNamespace(ns))) {
child.SetAttribute(name, value);
} else {
element.SetAttribute(name, ns, value);
}[/code]
<br/>
Of course one work around is to have a namespace prefix for all child instead of using no prefix.
View the full article
<pre lang="x-c# public class Sample
{
public static void Main()
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<root xmlns=urn:root>" +
"<child xmlns=urn:child/>" +
"</root>");
var child = doc.FirstChild.FirstChild as XmlElement;
string ns = "urn:root";
string prefix = child.GetPrefixOfNamespace(ns);
string nsFromPrefix = child.GetNamespaceOfPrefix(prefix);
Debug.Assert(ns == nsFromPrefix);
}
}[/code]
<br/>
From my point of view, there is a bug on the child.GetPrefixOfNamespace(ns) since in this case the prefix should not be empty when query on node child ? Maybe our XML is bad but I didnt find anything about it.
Our scenario was to add an attribute on the child node but within the root namespace. The child node was added by some function and can be in a different namespace and we were looking how to add the node for pretty printing, with something like :
<pre lang="x-c# if(string.IsNullOrEmpty(child.GetPrefixOfNamespace(ns))) {
child.SetAttribute(name, value);
} else {
element.SetAttribute(name, ns, value);
}[/code]
<br/>
Of course one work around is to have a namespace prefix for all child instead of using no prefix.
View the full article