473,378 Members | 1,623 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

using xml schema with xmlDOm in .net

hello 1
i use an xml documet with xml schema but when i try to use XPath
(selectnodes) it retrieves empty list.
when i remove the schema (the 'xmlns="http..."')
i get the data.
how can i handle this ?

using xmlDocument.documentElement.selectNodes("PROP")

something like this:
<OBJ xmlns="http://tempuri.org/obj.xsd">
<PROP NAME="x"/>
<PROP NAME="y"/>
</OBJ>
this not working (return empty nodelist)
but this:
<OBJ>
<PROP NAME="x"/>
<PROP NAME="y"/>
</OBJ>
is working and return the data.
Nov 20 '05 #1
3 2856
Hello Yair,

We had this same question a couple of weeks ago. I couldn't answer it then
but I investigated and a guy** in the dotnet.xml newsgroup helped an awful
lot. Now that I know, I'll try and put you right too.

Do you know about namespaces in xml - whereby you can have
<yair:foobar>...</yair:foobar> and <fergus:foobar>...</fergus:foobar> - the
'yair' and 'fergus' being the namespace prefixes ?

<example>
<yair:foobar>...</yair:foobar>
<fergus:foobar>...</fergus:foobar>
</example>

To do a search for <your> foobars the XPath would have to be
"//yair:foobar". A search for "//foobar" will return nothing because there are
no foobars without a namespace prefix.

That's pretty straightforward.

Now, when you introduce a schema using xmlns="http://tempuri.org/obj.xsd"
or some such, what you are doing is saying that <all> elements under the one
introducing the namespace, that don't have an explicit namespace prefix, will
have a default namespace (which is implicit and has an 'unnamed prefix').

Consider:
<example>
<foobar> (1) No namespace </foobar>
<lets_have_a_namespace
xmlns="FooBarTheElephant-Sep26th-1am">
<yair:foobar> (2) Yair namespace </yair:foobar>
<fergus:foobar> (3) Fergus namespace </fergus:foobar>
<foobar> (4) Default namespace </foobar>
</lets_have_a_namespace >
</example>

(1) This foobar has no namespace because it hasn't been given one and
there is no default in place.

(2), (3) These foobars have explicit namespaces. Can't miss 'em.

(4) This foobar has a default namespace which you can't see. It was
introduced in the enclosing element but it has no prefix. However, to find it
using XPath*, you <must provide> its prefix. Which is impossible!!!

The solution is to give it a prefix - not in the xml itself, as the whole
point of default namespaces is to save you having to add the namespace prefix
to every tag which doesn't have one - but in the code which does the
SelectNodes.

What you have to do is introduce a NamespaceManager.

oXmlDoc.Load ("C:\Foo\Bar.xml")
Dim oNamespaceMgr As XmlNamespaceManager _
= new XmlNamespaceManager (oXmlDoc.NameTable)
oNamespaceMgr.AddNamespace ("wow", "http://tempuri.org/obj.xsd")

Root = oXmlDoc.DocumentElement
NodeList = Root.SelectNodes ("//wow:foobar", oNamespaceMgr);

This time, using the example above, NodeList will contain (4) - the
unprefixed (default namespace) foobar.

The prefix 'wow' is totally arbitrary - you can give the NamespaceManager
any prefix you like - just so long as it doesn't clash with any other prefix
in the document and, of course, that you use it in your XPath.

The namespace given to the NamespaceManager must match the one in the
document <exactly> (ie. use cut and paste).

=======================================
About namespaces (for your info).

You are using the namespace "http://tempuri.org/obj.xsd". The thing about
namespaces is not that they mean anything but that they are arbitrary and
unique - practically anything is allowed. The reason that URLs are so often
given is that URLs are unique - if chosen well. If everyone uses the tempuri
URL, as you have, there could be clashes of namespaces when two xml documents
are merged. The advice is to use, or make up, a URL that would aply to you or
your company <only>. So http://Yair.com/my-unique-namespace/Sep26th-12-20am
would be pretty well unique. It doesn't even have to exist as a web site - it
is just an identifying string.

=======================================
Further reading:

This site has an excellent yet simple XML Namespaces FAQ.
http://www.rpbourret.com/xml/NamespacesFAQ.htm

There's also "XML Namespaces and How They Affect XPath and XSLT" by Dare
Obasanjo which is heavier reading. (I didn't finish it, lol, but I read it
before the other one)
http://msdn.microsoft.com/library/en...ml05202002.asp

Regards,
Fergus

=======================================
*XPath 1.0 has this default-namespace-requires-a-prefix-after-all issue. It
will be fixed in XPath 2.0

** Thanks to Oleg Tkachenko of dotnet.xml for teaching me a lot.
Nov 20 '05 #2
Hello Yair,

We had this same question a couple of weeks ago. I couldn't answer it then
but I investigated and a guy** in the dotnet.xml newsgroup helped an awful
lot. Now that I know, I'll try and put you right too.

Do you know about namespaces in xml - whereby you can have
<yair:foobar>...</yair:foobar> and <fergus:foobar>...</fergus:foobar> - the
'yair' and 'fergus' being the namespace prefixes ?

<example>
<yair:foobar>...</yair:foobar>
<fergus:foobar>...</fergus:foobar>
</example>

To do a search for <your> foobars the XPath would have to be
"//yair:foobar". A search for "//foobar" will return nothing because there are
no foobars without a namespace prefix.

That's pretty straightforward.

Now, when you introduce a schema using xmlns="http://tempuri.org/obj.xsd"
or some such, what you are doing is saying that <all> elements under the one
introducing the namespace, that don't have an explicit namespace prefix, will
have a default namespace (which is implicit and has an 'unnamed prefix').

Consider:
<example>
<foobar> (1) No namespace </foobar>
<lets_have_a_namespace
xmlns="FooBarTheElephant-Sep26th-1am">
<yair:foobar> (2) Yair namespace </yair:foobar>
<fergus:foobar> (3) Fergus namespace </fergus:foobar>
<foobar> (4) Default namespace </foobar>
</lets_have_a_namespace >
</example>

(1) This foobar has no namespace because it hasn't been given one and
there is no default in place.

(2), (3) These foobars have explicit namespaces. Can't miss 'em.

(4) This foobar has a default namespace which you can't see. It was
introduced in the enclosing element but it has no prefix. However, to find it
using XPath*, you <must provide> its prefix. Which is impossible!!!

The solution is to give it a prefix - not in the xml itself, as the whole
point of default namespaces is to save you having to add the namespace prefix
to every tag which doesn't have one - but in the code which does the
SelectNodes.

What you have to do is introduce a NamespaceManager.

oXmlDoc.Load ("C:\Foo\Bar.xml")
Dim oNamespaceMgr As XmlNamespaceManager _
= new XmlNamespaceManager (oXmlDoc.NameTable)
oNamespaceMgr.AddNamespace ("wow", "http://tempuri.org/obj.xsd")

Root = oXmlDoc.DocumentElement
NodeList = Root.SelectNodes ("//wow:foobar", oNamespaceMgr);

This time, using the example above, NodeList will contain (4) - the
unprefixed (default namespace) foobar.

The prefix 'wow' is totally arbitrary - you can give the NamespaceManager
any prefix you like - just so long as it doesn't clash with any other prefix
in the document and, of course, that you use it in your XPath.

The namespace given to the NamespaceManager must match the one in the
document <exactly> (ie. use cut and paste).

=======================================
About namespaces (for your info).

You are using the namespace "http://tempuri.org/obj.xsd". The thing about
namespaces is not that they mean anything but that they are arbitrary and
unique - practically anything is allowed. The reason that URLs are so often
given is that URLs are unique - if chosen well. If everyone uses the tempuri
URL, as you have, there could be clashes of namespaces when two xml documents
are merged. The advice is to use, or make up, a URL that would aply to you or
your company <only>. So http://Yair.com/my-unique-namespace/Sep26th-12-20am
would be pretty well unique. It doesn't even have to exist as a web site - it
is just an identifying string.

=======================================
Further reading:

This site has an excellent yet simple XML Namespaces FAQ.
http://www.rpbourret.com/xml/NamespacesFAQ.htm

There's also "XML Namespaces and How They Affect XPath and XSLT" by Dare
Obasanjo which is heavier reading. (I didn't finish it, lol, but I read it
before the other one)
http://msdn.microsoft.com/library/en...ml05202002.asp

Regards,
Fergus

=======================================
*XPath 1.0 has this default-namespace-requires-a-prefix-after-all issue. It
will be fixed in XPath 2.0

** Thanks to Oleg Tkachenko of dotnet.xml for teaching me a lot.

[2nd posting - 1st was deleted :-(]
Nov 20 '05 #3
Cor
Hi,
I ve seen this answer before
:-))
Cor
Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Shawn | last post by:
hi. I've created an xml document with Microsoft's XMLDOM. How can I validate the xml I've created against a schema (xsd) ? Thanks, Shawn
4
by: VK | last post by:
Dear All: I have an issue trying to parse response from xml document, for that matter I don't receive back response. I am trying to integrate UPS e-commerce online tool into our web site,...
2
by: Kenneth | last post by:
I am tryint to view a xml document in Internet Exploer as a ActiveX object using Javascript but it does not work. I only see the button but nothing happens when i click it. I am trying to view...
1
by: Henry Padilla | last post by:
I'm sorry if this has been asked and answered, I looked and didn't find this particular situation so I have to ask. If I understand correctly, the .NET XMLDOM must have an XmlNamespaceManager...
2
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I have the following code which I try to load an xml file, but in browser, I only get text value for each node, not whole xml string. I expect to see something like <name...
2
by: Joey Martin | last post by:
I'll try to make my explanation as thorough as possible. I am trying to grab the value of the node that is returned. MY CODE: Set xmlhttp = CreateObject("Microsoft.XMLHTTP") xmlhttp.open...
1
by: akumar8k | last post by:
when i am excusing the below code i am getting the error. Error 1 − <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> − <SOAP-ENV:Body> −
2
by: Steerman | last post by:
My Index.asp looks like this: <% 'Load XML set xml = Server.CreateObject("Microsoft.XMLDOM") xml.async = false xml.load(Server.MapPath("test.xml")) 'Load XSL set xsl =...
1
AnuSumesh
by: AnuSumesh | last post by:
Hi, I want to read the text property of XML file. My xml file is as follows: <?xml version="1.0"?> <Domain_Credentials> <User> anu </User>
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.