Re: Cannot find root element in XML file



"Martin Honnen" wrote:

TT (Tom Tempelaere) wrote:
Well to be honest I thought this would solve the problem, but it actually
doesn't. If instead of querying the 'IncidentDefinitions' root element I use
xmlDocument.DocumentElement I still have a problem: I can't find any other
nodes that are in the document.

Well I have shown you to use an XmlNamespaceManager

As for SelectSingleNode to find an element with XPath in a certain
namespace you would need e.g.
XmlNamespaceManager namespaceManager = new
XmlNamespaceManager(xmlDoc.NameTable);
namespaceManager.AddNamespace("pf", "http://tempuri.org/XMLSchema.xsd";);
XmlElement rootElement =
xmlDoc.SelectSingleNode("pf:IncidentDefinitions", namespaceManager) as
XmlElement;


Doesn't work either. It does return an element, but querying for any of the
documents nodes fails.

If those child or descendant elements are in the namespace
http://tempuri.org/XMLSchema.xsd then you again need to use the
namespace manager and you need to use the prefix in your XPath
expressions, that is how XPath works. I don't understand why you claim
the suggested ways don't work if you say they give you the element.

They don't work because from what I understood I only needed the namespace
thingy for querying the root node. The nodes in the XML file aren't qualified
so why would I have to qualify them to look them up?

Makes me wonder how I support both types of XML files, those with attributed
root and those without.

Anyway I don't really understand what it means: "If those child or
descendant elements are in the namespace
http://tempuri.org/XMLSchema.xsd...";. I'm far from being an expert in XML
(hence my questions) but I would say that the nodes that I defined in my
schema aren't "child" or "descendant" elements in the namespace
"http://tempuri.org/XMLSchema.xsd";. Or am I just not seeing things correctly?

If
you then write additional code that does not do what you want then that
additional code needs to be fixed obviously. Use that namespace manager,
use that prefix in your XPath expressions to qualify element names (e.g.
pf:elementname), pass the namespace manager to the SelectSingleNode or
SelectNodes method calls and if you have the proper path then you will
find nodes.

Well my code that reads in the XML works fine if the root node doesn't have
the attributes, so my code isn't wrong per se.

Anyway, since I'm too noob to understand your answers, I'll go the easy way.
Away with those stupid attributes.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

--
Tom Tempelaere.
.



Relevant Pages


Quantcast