Well you don't need to know the exact prefixes - but you do need to know the
Uri's. The prefixes are extended to full URI's anyway so if the prefix in
your document is "myns" associated with the uri
http://someuri.com/ and in
your .Net class you associate the prefix "yourns" with the URI
"http:someuri.com/" then there is no difference. myns and yourns are
expanded to the same URI.
As for URI's - yes this is true. But this only makes sense. If you define an
Xml element within the scope of some URI you are effectively defining its
type. Although two elements may have the same name, if they ahve different
namesapces they are twop completely different things. Can you imagine in the
..Net world just creating an instance of "class1"? This is the default class
that is created with every new project in VS.Net - imagine you just
compliled all of them with no namespaces. Just a a whole bunch of "class1"'s
in the same directly. (Imagine...) If you were able to create an instance -
what class1 are you talking about? The key is that namespaces can be used to
differentiate the significance of different classes that have the same name.
For some reason in the Xml world people just don't seem to see this as being
effectively the same thing.
I guess Xml without namespaces would be like "DLL hell" on windows :)
Also, sure you can reflect against the Xml doc and get the namespaces and
build your NSManager from there. However, how often do you actually ever do
a SelectNode... without knowing the namespace (or type) of the node you are
selecting?
Well, you asked for my thoughts :)
Cheers,
Steven
"Colin Green" <bu*******@hotmail.com> wrote in message
news:a2**************************@posting.google.c om...
I have come across what seems like a failing in the .Net XML classes.
Many people have posted requesting how to write an XPath query with
namespace prefixes that works. The solution shown in all the posts I
have seen is of the form:
---------------------------------------------
XmlDocument doc = new XmlDocument();
doc.Load(@"c:\foobar.xml");
XmlNamepaceManager nmMgr = newNamespaceManager(doc);
nmMgr.AddNamespace("abc", "www.my.uri");
---------------------------------------------
Now you can call SelectSingleNode and SelectNodes with an XPath query
that contains the abc prefix.
What concerns me is that this manual registering of prefixes is
necessary even if the prefix was already defined in the source
document, e.g.
<abc foo xmlns:abc="www.my.uri"/>
Therefore the solution is only good if you are dealing with a known
set of uri's and prefixes. If (in my case) you are dealing with
arbitrary documents with arbitrary uri's and prefixes defined then the
solution is of no use.
AFAIK the only way to recognise prefixes declared within a document is
to write your own code to traverse the document, extract the
prefix/uri pairs and /then/ register them with an XmlNamespaceManager.
Thoughts?
Cheers,
Colin Green