473,387 Members | 1,606 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,387 software developers and data experts.

Selecting XML nodes w/ namespace and XElement

I'm playing around with XElement stuff, and I've come across a difficulty.
The XML document that I'm reading contains an xmlns declaration on the main
node...

<root xmlns="http://www.me.com">
<ANode>
<BNode>Hello</BNode>
<BNode>Goodbye</BNode>
</ANode>
</root>

I want to be able to query this document using XPath. I can successfully
do this with the following:

XmlDocument doc = new XmlDocument();
doc.LoadXml(s);
XmlNamespaceManager nsm = new XmlNamespaceManager(doc.NameTable);
nsm.AddNamespace("t", "http://www.me.com");
int n = doc.SelectNodes("/t:root/t:ANode/t:BNode", nsm).Count;

.... and I see that n = 2.

But when I try to convert this to XElement (and who knows I may be
WWWAAAYYY off here as I'm just starting to look at this):

XmlReader rdr = XmlReader.Create(new StringReader(s));
XElement x = XElement.Load(rdr);
XmlNamespaceManager nsm = new XmlNamespaceManager(rdr.NameTable);
nsm.AddNamespace("t", "http://www.me.com");
int n = x.XPathSelectElements("/t:root/t:ANode/t:BNode", nsm).Count();

.... I get n = 0.
What am I doing wrong?

-mdb

Jul 9 '08 #1
11 9829
"Michael Bray" <mb***@dontemailme.comwrote in message
news:Xn*****************@207.46.248.16...
I'm playing around with XElement stuff, and I've come across a difficulty.
The XML document that I'm reading contains an xmlns declaration on the
main
node...

<root xmlns="http://www.me.com">
<ANode>
<BNode>Hello</BNode>
<BNode>Goodbye</BNode>
</ANode>
</root>

I want to be able to query this document using XPath. I can successfully
do this with the following:

XmlDocument doc = new XmlDocument();
doc.LoadXml(s);
XmlNamespaceManager nsm = new XmlNamespaceManager(doc.NameTable);
nsm.AddNamespace("t", "http://www.me.com");
int n = doc.SelectNodes("/t:root/t:ANode/t:BNode", nsm).Count;

... and I see that n = 2.

But when I try to convert this to XElement (and who knows I may be
WWWAAAYYY off here as I'm just starting to look at this):

XmlReader rdr = XmlReader.Create(new StringReader(s));
XElement x = XElement.Load(rdr);
XmlNamespaceManager nsm = new XmlNamespaceManager(rdr.NameTable);
nsm.AddNamespace("t", "http://www.me.com");
int n = x.XPathSelectElements("/t:root/t:ANode/t:BNode", nsm).Count();

... I get n = 0.
What am I doing wrong?
I've not done much with this new XElement either but I'd be willing to guess
that since x is root and in this case is the top most node (in the
XmlDocument the document is the top most node and root is a child). then
the path is wrong. / refers to root and root doesn't have a child called
root.

Try:-

"t:ANode/t:BNode"

I'm currently on a my 2000 machine so I can't test that at the moment.

--
Anthony Jones - MVP ASP/ASP.NET
Jul 9 '08 #2
Dude,

First, understand the difference between XMLDocument and XMLReader.
XMLDocument is inmemory .. where are XMLReader is not a cache.

thus, when you first queried you got the count, where are when you did the
second mechanism, it doesn't have loaded the entire XML into memory.

Hope am clear to you,


"Michael Bray" <mb***@dontemailme.comwrote in message
news:Xn*****************@207.46.248.16...
I'm playing around with XElement stuff, and I've come across a difficulty.
The XML document that I'm reading contains an xmlns declaration on the
main
node...

<root xmlns="http://www.me.com">
<ANode>
<BNode>Hello</BNode>
<BNode>Goodbye</BNode>
</ANode>
</root>

I want to be able to query this document using XPath. I can successfully
do this with the following:

XmlDocument doc = new XmlDocument();
doc.LoadXml(s);
XmlNamespaceManager nsm = new XmlNamespaceManager(doc.NameTable);
nsm.AddNamespace("t", "http://www.me.com");
int n = doc.SelectNodes("/t:root/t:ANode/t:BNode", nsm).Count;

... and I see that n = 2.

But when I try to convert this to XElement (and who knows I may be
WWWAAAYYY off here as I'm just starting to look at this):

XmlReader rdr = XmlReader.Create(new StringReader(s));
XElement x = XElement.Load(rdr);
XmlNamespaceManager nsm = new XmlNamespaceManager(rdr.NameTable);
nsm.AddNamespace("t", "http://www.me.com");
int n = x.XPathSelectElements("/t:root/t:ANode/t:BNode", nsm).Count();

... I get n = 0.
What am I doing wrong?

-mdb
Jul 10 '08 #3
On Jul 10, 12:24*pm, "Chakravarthy" <dskch...@msn.comwrote:
First, understand the difference between XMLDocument and XMLReader.
XMLDocument is inmemory .. where are XMLReader is not a cache.

thus, when you first queried you got the count, where are when you did the
second mechanism, it doesn't have loaded the entire XML into memory.
Doesn't XElement.Load(rdr) load the whole thing into memory though?
That's certainly what I'd expect.

Jon
Jul 10 '08 #4
On Jul 10, 12:45*am, "Anthony Jones" <A...@yadayadayada.comwrote:
"Michael Bray" <mb...@dontemailme.comwrote in message

news:Xn*****************@207.46.248.16...


I'm playing around with XElement stuff, and I've come across a difficulty.
The XML document that I'm reading contains an xmlns declaration on the
main
node...
<root xmlns="http://www.me.com">
* *<ANode>
* * * <BNode>Hello</BNode>
* * * <BNode>Goodbye</BNode>
* *</ANode>
</root>
I want to be able to query this document using XPath. *I can successfully
do this with the following:
XmlDocument doc = new XmlDocument();
doc.LoadXml(s);
XmlNamespaceManager nsm = new XmlNamespaceManager(doc.NameTable);
nsm.AddNamespace("t", "http://www.me.com");
int n = doc.SelectNodes("/t:root/t:ANode/t:BNode", nsm).Count;
... *and I see that n = 2.
But when I try to convert this to XElement (and who knows I may be
WWWAAAYYY off here as I'm just starting to look at this):
XmlReader rdr = XmlReader.Create(new StringReader(s));
XElement x = XElement.Load(rdr);
XmlNamespaceManager nsm = new XmlNamespaceManager(rdr.NameTable);
nsm.AddNamespace("t", "http://www.me.com");
int n = x.XPathSelectElements("/t:root/t:ANode/t:BNode", nsm).Count();
... *I get n = 0.
What am I doing wrong?

I've not done much with this new XElement either but I'd be willing to guess
that since x is root and in this case is the top most node (in the
XmlDocument the document is the top most node and root is a child). *then
the path is wrong. */ refers to root and root doesn't have a child called
root.

*Try:-

"t:ANode/t:BNode"
Yes, this is correct - "/" refers to the root node in the XML tree,
which is document in XmlDocument, but <rootelement in this case.
You may either follow the suggestion above, or load the XML into
System.Xml.Linq.XDocument instead - then you'll get the usual
behavior.

By the way, why XPath? The whole point of the new APIs is to use LINQ
queries instead:

XNamespace t = "http://www.me.com";
var result = doc.Elements(t + "ANode").Elements(t + "BNode");
Jul 10 '08 #5
Pavel Minaev <in****@gmail.comwrote in news:ec4a9b96-77b8-4035-bbe3-
ac**********@k13g2000hse.googlegroups.com:
Yes, this is correct - "/" refers to the root node in the XML tree,
which is document in XmlDocument, but <rootelement in this case.
You may either follow the suggestion above, or load the XML into
System.Xml.Linq.XDocument instead - then you'll get the usual
behavior.

By the way, why XPath? The whole point of the new APIs is to use LINQ
queries instead:

XNamespace t = "http://www.me.com";
var result = doc.Elements(t + "ANode").Elements(t + "BNode");
I'm using XPath because I want to be able to specify in a configuration
file which set of nodes to analyze. What you say makes a lot of sense if I
always know the path and can hard code it.

Even so, testing your code yields no results:

string s = "<root xmlns='http://www.me.com'><ANode><BNode>Hello</BNode>
<BNode>Goodbye</BNode></ANode></root>";

XDocument doc = XDocument.Parse(s);
XNamespace ns = "http://www.me.com";
int nn = doc.Elements(ns + "ANode").Elements(ns + "BNode").Count();

yields.. nn = 0. did I miss something?

-mdb
Jul 10 '08 #6
"Anthony Jones" <An*@yadayadayada.comwrote in
news:##**************@TK2MSFTNGP05.phx.gbl:
I've not done much with this new XElement either but I'd be willing to
guess that since x is root and in this case is the top most node (in
the XmlDocument the document is the top most node and root is a
child). then the path is wrong. / refers to root and root doesn't
have a child called root.

Try:-

"t:ANode/t:BNode"
Yup tried that before I posted (along with every other path I could think
of).. Same result - 0 nodes selected.

I'm just grasping at straws at this point - almost ready to abandon XML
Linq for now and stick with what works.

-mdb
Jul 10 '08 #7
Michael Bray wrote:
Even so, testing your code yields no results:

string s = "<root xmlns='http://www.me.com'><ANode><BNode>Hello</BNode>
<BNode>Goodbye</BNode></ANode></root>";

XDocument doc = XDocument.Parse(s);
XNamespace ns = "http://www.me.com";
int nn = doc.Elements(ns + "ANode").Elements(ns + "BNode").Count();

yields.. nn = 0. did I miss something?
Yes, certainly, the XDocument doc you construct has a root element named
'root' that your code ignores. You need/want
doc.Root.Elements(ns + "ANode").Elements(ns + "BNode").Count()
or (naming the root explicitly)
doc.Element(ns + "root").Elements(ns + "ANode").Elements(ns +
"BNode").Count()

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jul 10 '08 #8
Michael Bray wrote:
"Anthony Jones" <An*@yadayadayada.comwrote in
news:##**************@TK2MSFTNGP05.phx.gbl:
>I've not done much with this new XElement either but I'd be willing to
guess that since x is root and in this case is the top most node (in
the XmlDocument the document is the top most node and root is a
child). then the path is wrong. / refers to root and root doesn't
have a child called root.

Try:-

"t:ANode/t:BNode"

Yup tried that before I posted (along with every other path I could think
of).. Same result - 0 nodes selected.
Here is a working sample that outputs 2:

XElement root = XElement.Load(@"..\..\XMLFile1.xml");
XmlNamespaceManager mgr = new XmlNamespaceManager(new
NameTable());
mgr.AddNamespace("pf", "http://www.me.com");

Console.WriteLine(root.XPathSelectElements("pf:ANo de/pf:BNode",
mgr).Count());

XML document is

<?xml version="1.0" encoding="utf-8" ?>
<root xmlns="http://www.me.com">
<ANode>
<BNode>Hello</BNode>
<BNode>Goodbye</BNode>
</ANode>
</root>

If your code does not find anything then please show us a complete
sample that demonstrates that, somewhere there must be a mistake.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jul 10 '08 #9
"Anthony Jones" <An*@yadayadayada.comwrote in
news:##**************@TK2MSFTNGP05.phx.gbl:
I've not done much with this new XElement either but I'd be willing to
guess that since x is root and in this case is the top most node (in
the XmlDocument the document is the top most node and root is a
child). then the path is wrong. / refers to root and root doesn't
have a child called root.

Try:-

"t:ANode/t:BNode"

OK hmmm.. as I was playing around I think I fixed it... I can query
using both the XPath query mode and the Elements(..)... Here's the code
that works for me:

string s = "<root xmlns='http://www.me.com'><ANode><BNode>Hello</BNode>
<BNode>Goodbye</BNode></ANode></root>";

XmlReader rdr = XmlReader.Create(new StringReader(s));
XDocument doc = XDocument.Load(rdr);
XmlNamespaceManager nsm = new XmlNamespaceManager(rdr.NameTable);
nsm.AddNamespace("t", "http://www.me.com");
XNamespace ns = "http://www.me.com";

int n = doc.XPathSelectElements("/t:root/t:ANode/t:BNode", nsm).Count();
int nn = doc.Elements(ns + "root").Elements(ns + "ANode").Elements(ns +
"BNode").Count();
int nnn = doc.Elements("root").Elements("ANode").Elements("B Node").Count();

var q = doc.XPathSelectElements("/t:root/t:ANode/t:BNode", nsm);
foreach (var v in q) Console.WriteLine(v.Value);
yields.... n = 2 nn = 2 nnn = 0
and outputs: Hello Goodbye

so I think the main thing that fixed it was loading into XDocument instead
of XElement. Also, when querying using the Elements() I needed to make
sure I added the XNamespace.

Thanks to everyone for their input... I knew it would be something simple
I was missing.

-mdb

Jul 10 '08 #10
Martin Honnen <ma*******@yahoo.dewrote in news:#3LFOoq4IHA.4340
@TK2MSFTNGP06.phx.gbl:
Yes, certainly, the XDocument doc you construct has a root element named
'root' that your code ignores. You need/want
doc.Root.Elements(ns + "ANode").Elements(ns + "BNode").Count()
or (naming the root explicitly)
doc.Element(ns + "root").Elements(ns + "ANode").Elements(ns +
"BNode").Count()
Haha yeah I actually just meandered into the same solution and posted the
same answer... Turned out that my original problem was that I was loading
into XElement instead of XDocument!

Thanks for you help!!

-mdb
Jul 10 '08 #11
Martin Honnen <ma*******@yahoo.dewrote in news:uTBS$pq4IHA.4340
@TK2MSFTNGP06.phx.gbl:
If your code does not find anything then please show us a complete
sample that demonstrates that, somewhere there must be a mistake.
Hmmm now that is odd... I confirm that this works, but I can't see any
significant difference from what (I thought) I tested... maybe I just
was too blinded by frustration and was making silly mistakes.

Oh well... I have things working like I need them now, so I'm happy again.
Thanks!

-mdb
Jul 10 '08 #12

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

Similar topics

2
by: kj | last post by:
Suppose I have some XML document that contains tags of the form <... xmlns:foo="http://www.bar.org/foo"> <... xmlns:foo="baz"> <... xmlns:frobozz="http://www.bar.org/foo"> What's the...
2
by: Dag | last post by:
Hi I am pretty much an xml beginner; hopefully someone can easily answer this one... I want to select a node representing a worksheet in an xml document for the Office Web Components spreadsheet....
6
by: Nikhil Patel | last post by:
Hi all, Following is a portion of an XML document. I need to remove all nodes that belong to ns0 without deleting their child nodes. So in the following example , I want to delete "ns0:Proposal"...
12
by: Lawrence Oluyede | last post by:
I've some problems with filtering out a specific element by the following xml snippet (is part of a bigger xml document): <entry> <link...
1
by: Cheryl | last post by:
I have problems selecting the attribute when I use the SelectNode function. <book> <title store='7456' >Nutshell</title> <author id='1'>Drayton</author> <xauthor publish = "1" publishtime =...
2
by: Tymbow | last post by:
I'm building a web application that is analogous to the Windows XP file explorer in function. The left column contains a TreeView, and the right column a DataGrid populated by selecting TreeView...
1
by: =?Utf-8?B?QWxleGFuZGVyIFd5a2Vs?= | last post by:
Thank you Wen Yuan, This did answer my question, but it took a lot of work to implement. My original code did not use any namespace so I had to modify and test some 3000 lines of code to make...
1
by: egholm | last post by:
I'm using the following XML: ---------------------------------------------------------------------------­-------------- <?xml version="1.0" encoding="utf-8" ?> <Elements...
2
by: SR | last post by:
How can I remove an empty namespace with XElement ? Using the below code I wish to apply the namespace only to the root node <urlsetbut I obtain also an empty xmlns="" on the child nodes. Thanks...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.