[Help]It returns 0 when using C# to process XML.

W

wishsand

Here is the XML in a file:
<?xml version='1.0' encoding='utf-8' ?>
<bkxml version='1.0' xmlns='http://www.w3.org/2003/03/bkxml'>
<model>
<data name="angle" expr="100"></data>
<data name="sample" expr="200"></data>
</model>
</bkxml>


I use
XmlDocument.DocumentElement.SelectNode("/bkxml/model/data").Count,but
it returns 0. It will work once xmlns is omitted. However I have
defined Bkxml.xsd well, and validation is ok.

Who have experienced the same problem? Could you explain the reason?

Thank you!

Sand
 
J

Johannes Koch

Here is the XML in a file:
<?xml version='1.0' encoding='utf-8' ?>
<bkxml version='1.0' xmlns='http://www.w3.org/2003/03/bkxml'>
<model>
<data name="angle" expr="100"></data>
<data name="sample" expr="200"></data>
</model>
</bkxml>


I use
XmlDocument.DocumentElement.SelectNode("/bkxml/model/data").Count,but
it returns 0. It will work once xmlns is omitted. However I have
defined Bkxml.xsd well, and validation is ok.

Who have experienced the same problem? Could you explain the reason?

Namespace issue. You select nodes from the null namespace. But the
elements in your XML are in the 'http://www.w3.org/2003/03/bkxml'
namespace. Read up about mapping prefixes to namespace URIs and then use
a prefix in the XPath.
 
J

Joseph Kesselman

XmlDocument.DocumentElement.SelectNode("/bkxml/model/data").Count,but
it returns 0. It will work once xmlns is omitted.

XPaths are namespace-aware. You must use a prefix in your XPath and
provide the XPath evaluator with context information that binds that
prefix to the appropriate namespace, even if the default-namespace
syntax was used in the document. The only alternative is the ugly one of
writing XPaths with wildcards and then using predicates to test their
names -- which, among its other flaws, runs less efficiently in some
processors.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,005
Messages
2,570,264
Members
46,859
Latest member
HeidiAtkin

Latest Threads

Top