XmlElement.SelectNodes: Need help with accessing xml nodes

G

Guest

Any help will be really appreciated.

When I use

XmlNodeList nodeList;
XmlElement root = doc.DocumentElement;
nodeList = root.SelectNodes("/bookstore/book");

to select nodes matching the XPath string, from XML file below, I get one
node which is OK.

BUT, if I remove the ":bk" from the attribute of <bookstore> node, I will
not get any nodes. WHY? I want to be able to do that. Thank you for your help.

<?xml version="1.0" encoding="utf-8" ?>
<bookstore xmlns:bk="urn:samples">
<book>
<title>Pride And Prejudice</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>24.95</price>
</book>
</bookstore>
 
M

Matt Berther

Hello Nad,

Because the xmlns:bk is defining the namespace which your XPATH (/bookstore/book)
is not using. To make it work, you'll need to define an XmlNamespaceManager,
add the namespaces to it and then pass that instance to SelectNodes. You'll
also need to update your XPATH to use the namespace.

[C#]
XmlElement root = doc.DocumentElement;
XmlNamespaceManager nsManager = new XmlNamespaceManager();
nsManager.AddNamespace("bk", "urn:samples");
XmlNodeList nodeList = root.SelectNodes("/bk:bookstore/bk:book", nsManager);
 
G

Guest

Thanks Matt,

I am aware of that. The thing is I don't want to have ":bk" in my xml file.
And without that I can't get SelectNodes to work.

Matt Berther said:
Hello Nad,

Because the xmlns:bk is defining the namespace which your XPATH (/bookstore/book)
is not using. To make it work, you'll need to define an XmlNamespaceManager,
add the namespaces to it and then pass that instance to SelectNodes. You'll
also need to update your XPATH to use the namespace.

[C#]
XmlElement root = doc.DocumentElement;
XmlNamespaceManager nsManager = new XmlNamespaceManager();
nsManager.AddNamespace("bk", "urn:samples");
XmlNodeList nodeList = root.SelectNodes("/bk:bookstore/bk:book", nsManager);

--
Matt Berther
http://www.mattberther.com
Any help will be really appreciated.

When I use

XmlNodeList nodeList;
XmlElement root = doc.DocumentElement;
nodeList = root.SelectNodes("/bookstore/book");
to select nodes matching the XPath string, from XML file below, I get
one node which is OK.

BUT, if I remove the ":bk" from the attribute of <bookstore> node, I
will not get any nodes. WHY? I want to be able to do that. Thank you
for your help.

<?xml version="1.0" encoding="utf-8" ?>
<bookstore xmlns:bk="urn:samples">
<book>
<title>Pride And Prejudice</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>24.95</price>
</book>
</bookstore>
 
M

Matt Berther

Hello Nad,

So, assuming that when you say you remove :bk you're still stuck with this
xml

<bookstore xmlns="urn:samples">

My previous post is still accurate. xmlns defines the default namespace.
If you dont want a namespace, then remove the whole declaration.

--
Matt Berther
http://www.mattberther.com
Thanks Matt,

I am aware of that. The thing is I don't want to have ":bk" in my xml
file. And without that I can't get SelectNodes to work.

Matt Berther said:
Hello Nad,

Because the xmlns:bk is defining the namespace which your XPATH
(/bookstore/book) is not using. To make it work, you'll need to
define an XmlNamespaceManager, add the namespaces to it and then pass
that instance to SelectNodes. You'll also need to update your XPATH
to use the namespace.

[C#]
XmlElement root = doc.DocumentElement;
XmlNamespaceManager nsManager = new XmlNamespaceManager();
nsManager.AddNamespace("bk", "urn:samples");
XmlNodeList nodeList = root.SelectNodes("/bk:bookstore/bk:book",
nsManager);
--
Matt Berther
http://www.mattberther.com
Any help will be really appreciated.

When I use

XmlNodeList nodeList;
XmlElement root = doc.DocumentElement;
nodeList = root.SelectNodes("/bookstore/book");
to select nodes matching the XPath string, from XML file below, I
get
one node which is OK.
BUT, if I remove the ":bk" from the attribute of <bookstore> node, I
will not get any nodes. WHY? I want to be able to do that. Thank you
for your help.

<?xml version="1.0" encoding="utf-8" ?>
<bookstore xmlns:bk="urn:samples">
<book>
<title>Pride And Prejudice</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>24.95</price>
</book>
</bookstore>
 
G

Guest

But I need the namespace. Here is the real scenario.

I have an xml file that contains the data related to XmlSchema.
My real xml file looks something like this:

<?xml version="1.0" encoding="utf-8"?>
<ClientsVisits xmlns="http://tempuri.org/ClientsVisits.xsd">
<Clients>
....
</Clients>
</ClientsVisits>

And I am not able to get any nodes using SelectNodes. But if I change the
namespace to something like xmlns:bk="http://tempuri.org/ClientsVisits.xsd"
it works but in this case the xml loses its reference to the schema and my
datagrid is not even populated.

Hope it's clear.


Matt Berther said:
Hello Nad,

So, assuming that when you say you remove :bk you're still stuck with this
xml

<bookstore xmlns="urn:samples">

My previous post is still accurate. xmlns defines the default namespace.
If you dont want a namespace, then remove the whole declaration.

--
Matt Berther
http://www.mattberther.com
Thanks Matt,

I am aware of that. The thing is I don't want to have ":bk" in my xml
file. And without that I can't get SelectNodes to work.

Matt Berther said:
Hello Nad,

Because the xmlns:bk is defining the namespace which your XPATH
(/bookstore/book) is not using. To make it work, you'll need to
define an XmlNamespaceManager, add the namespaces to it and then pass
that instance to SelectNodes. You'll also need to update your XPATH
to use the namespace.

[C#]
XmlElement root = doc.DocumentElement;
XmlNamespaceManager nsManager = new XmlNamespaceManager();
nsManager.AddNamespace("bk", "urn:samples");
XmlNodeList nodeList = root.SelectNodes("/bk:bookstore/bk:book",
nsManager);
--
Matt Berther
http://www.mattberther.com
Any help will be really appreciated.

When I use

XmlNodeList nodeList;
XmlElement root = doc.DocumentElement;
nodeList = root.SelectNodes("/bookstore/book");
to select nodes matching the XPath string, from XML file below, I
get
one node which is OK.
BUT, if I remove the ":bk" from the attribute of <bookstore> node, I
will not get any nodes. WHY? I want to be able to do that. Thank you
for your help.

<?xml version="1.0" encoding="utf-8" ?>
<bookstore xmlns:bk="urn:samples">
<book>
<title>Pride And Prejudice</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>24.95</price>
</book>
</bookstore>
 
M

Matt Berther

Hello Nad,

So, you'll need to map the namespace to a prefix used by your xpath, using
the XmlNamespaceManager...

[C#]
XmlElement root = doc.DocumentElement;
XmlNamespaceManager nsManager = new XmlNamespaceManager();
nsManager.AddNamespace("someArbitraryPrefix", "http://tempuri.org/ClientsVisits.xsd");
XmlNodeList nodeList = root.SelectNodes("/someArbitraryPrefix:bookstore/someArbitraryPrefix:book",
nsManager);

Notice that the prefix that you register with the nsManager does not have
to be in the actual document itself, just the namespace.

--
Matt Berther
http://www.mattberther.com
But I need the namespace. Here is the real scenario.

I have an xml file that contains the data related to XmlSchema. My
real xml file looks something like this:

<?xml version="1.0" encoding="utf-8"?>
<ClientsVisits xmlns="http://tempuri.org/ClientsVisits.xsd">
<Clients>
...
</Clients>
</ClientsVisits>
And I am not able to get any nodes using SelectNodes. But if I change
the namespace to something like
xmlns:bk="http://tempuri.org/ClientsVisits.xsd" it works but in this
case the xml loses its reference to the schema and my datagrid is not
even populated.

Hope it's clear.

Matt Berther said:
Hello Nad,

So, assuming that when you say you remove :bk you're still stuck with
this xml

<bookstore xmlns="urn:samples">

My previous post is still accurate. xmlns defines the default
namespace. If you dont want a namespace, then remove the whole
declaration.

--
Matt Berther
http://www.mattberther.com
Thanks Matt,

I am aware of that. The thing is I don't want to have ":bk" in my
xml file. And without that I can't get SelectNodes to work.

:

Hello Nad,

Because the xmlns:bk is defining the namespace which your XPATH
(/bookstore/book) is not using. To make it work, you'll need to
define an XmlNamespaceManager, add the namespaces to it and then
pass that instance to SelectNodes. You'll also need to update your
XPATH to use the namespace.

[C#]
XmlElement root = doc.DocumentElement;
XmlNamespaceManager nsManager = new XmlNamespaceManager();
nsManager.AddNamespace("bk", "urn:samples");
XmlNodeList nodeList = root.SelectNodes("/bk:bookstore/bk:book",
nsManager);
--
Matt Berther
http://www.mattberther.com
Any help will be really appreciated.

When I use

XmlNodeList nodeList;
XmlElement root = doc.DocumentElement;
nodeList = root.SelectNodes("/bookstore/book");
to select nodes matching the XPath string, from XML file below, I
get
one node which is OK.
BUT, if I remove the ":bk" from the attribute of <bookstore> node,
I
will not get any nodes. WHY? I want to be able to do that. Thank
you
for your help.
<?xml version="1.0" encoding="utf-8" ?>
<bookstore xmlns:bk="urn:samples">
<book>
<title>Pride And Prejudice</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>24.95</price>
</book>
</bookstore>
 
G

Guest

Great!

Thanks a lot. I'm happy, I'm happy, I'm happy,...

Matt Berther said:
Hello Nad,

So, you'll need to map the namespace to a prefix used by your xpath, using
the XmlNamespaceManager...

[C#]
XmlElement root = doc.DocumentElement;
XmlNamespaceManager nsManager = new XmlNamespaceManager();
nsManager.AddNamespace("someArbitraryPrefix", "http://tempuri.org/ClientsVisits.xsd");
XmlNodeList nodeList = root.SelectNodes("/someArbitraryPrefix:bookstore/someArbitraryPrefix:book",
nsManager);

Notice that the prefix that you register with the nsManager does not have
to be in the actual document itself, just the namespace.

--
Matt Berther
http://www.mattberther.com
But I need the namespace. Here is the real scenario.

I have an xml file that contains the data related to XmlSchema. My
real xml file looks something like this:

<?xml version="1.0" encoding="utf-8"?>
<ClientsVisits xmlns="http://tempuri.org/ClientsVisits.xsd">
<Clients>
...
</Clients>
</ClientsVisits>
And I am not able to get any nodes using SelectNodes. But if I change
the namespace to something like
xmlns:bk="http://tempuri.org/ClientsVisits.xsd" it works but in this
case the xml loses its reference to the schema and my datagrid is not
even populated.

Hope it's clear.

Matt Berther said:
Hello Nad,

So, assuming that when you say you remove :bk you're still stuck with
this xml

<bookstore xmlns="urn:samples">

My previous post is still accurate. xmlns defines the default
namespace. If you dont want a namespace, then remove the whole
declaration.

--
Matt Berther
http://www.mattberther.com
Thanks Matt,

I am aware of that. The thing is I don't want to have ":bk" in my
xml file. And without that I can't get SelectNodes to work.

:

Hello Nad,

Because the xmlns:bk is defining the namespace which your XPATH
(/bookstore/book) is not using. To make it work, you'll need to
define an XmlNamespaceManager, add the namespaces to it and then
pass that instance to SelectNodes. You'll also need to update your
XPATH to use the namespace.

[C#]
XmlElement root = doc.DocumentElement;
XmlNamespaceManager nsManager = new XmlNamespaceManager();
nsManager.AddNamespace("bk", "urn:samples");
XmlNodeList nodeList = root.SelectNodes("/bk:bookstore/bk:book",
nsManager);
--
Matt Berther
http://www.mattberther.com
Any help will be really appreciated.

When I use

XmlNodeList nodeList;
XmlElement root = doc.DocumentElement;
nodeList = root.SelectNodes("/bookstore/book");
to select nodes matching the XPath string, from XML file below, I
get
one node which is OK.
BUT, if I remove the ":bk" from the attribute of <bookstore> node,
I
will not get any nodes. WHY? I want to be able to do that. Thank
you
for your help.
<?xml version="1.0" encoding="utf-8" ?>
<bookstore xmlns:bk="urn:samples">
<book>
<title>Pride And Prejudice</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>24.95</price>
</book>
</bookstore>
 

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

Similar Threads


Top