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>Goodby e</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);
XmlNamespaceMan ager nsm = new XmlNamespaceMan ager(doc.NameTa ble);
nsm.AddNamespac e("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.Creat e(new StringReader(s) );
XElement x = XElement.Load(r dr);
XmlNamespaceMan ager nsm = new XmlNamespaceMan ager(rdr.NameTa ble);
nsm.AddNamespac e("t", "http://www.me.com");
int n = x.XPathSelectEl ements("/t:root/t:ANode/t:BNode", nsm).Count();
.... I get n = 0.
What am I doing wrong?
-mdb 11 9845
"Michael Bray" <mb***@dontemai lme.comwrote in message
news:Xn******** *********@207.4 6.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>Goodby e</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);
XmlNamespaceMan ager nsm = new XmlNamespaceMan ager(doc.NameTa ble);
nsm.AddNamespac e("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.Creat e(new StringReader(s) );
XElement x = XElement.Load(r dr);
XmlNamespaceMan ager nsm = new XmlNamespaceMan ager(rdr.NameTa ble);
nsm.AddNamespac e("t", "http://www.me.com");
int n = x.XPathSelectEl ements("/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
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***@dontemai lme.comwrote in message
news:Xn******** *********@207.4 6.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>Goodby e</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);
XmlNamespaceMan ager nsm = new XmlNamespaceMan ager(doc.NameTa ble);
nsm.AddNamespac e("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.Creat e(new StringReader(s) );
XElement x = XElement.Load(r dr);
XmlNamespaceMan ager nsm = new XmlNamespaceMan ager(rdr.NameTa ble);
nsm.AddNamespac e("t", "http://www.me.com");
int n = x.XPathSelectEl ements("/t:root/t:ANode/t:BNode", nsm).Count();
... I get n = 0.
What am I doing wrong?
-mdb
On Jul 10, 12:24*pm, "Chakravart hy" <dskch...@msn.c omwrote:
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(r dr) load the whole thing into memory though?
That's certainly what I'd expect.
Jon
On Jul 10, 12:45*am, "Anthony Jones" <A...@yadayaday ada.comwrote:
"Michael Bray" <mb...@dontemai lme.comwrote in message
news:Xn******** *********@207.4 6.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>Goodby e</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);
XmlNamespaceMan ager nsm = new XmlNamespaceMan ager(doc.NameTa ble);
nsm.AddNamespac e("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.Creat e(new StringReader(s) );
XElement x = XElement.Load(r dr);
XmlNamespaceMan ager nsm = new XmlNamespaceMan ager(rdr.NameTa ble);
nsm.AddNamespac e("t", "http://www.me.com");
int n = x.XPathSelectEl ements("/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").Elemen ts(t + "BNode");
Pavel Minaev <in****@gmail.c omwrote in news:ec4a9b96-77b8-4035-bbe3- ac**********@k1 3g2000hse.googl egroups.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").Elemen ts(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'><AN ode><BNode>Hell o</BNode>
<BNode>Goodby e</BNode></ANode></root>";
XDocument doc = XDocument.Parse (s);
XNamespace ns = "http://www.me.com";
int nn = doc.Elements(ns + "ANode").Elemen ts(ns + "BNode").Count( );
yields.. nn = 0. did I miss something?
-mdb
"Anthony Jones" <An*@yadayadaya da.comwrote in
news:##******** ******@TK2MSFTN GP05.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
Michael Bray wrote:
Even so, testing your code yields no results:
string s = "<root xmlns='http://www.me.com'><AN ode><BNode>Hell o</BNode>
<BNode>Goodby e</BNode></ANode></root>";
XDocument doc = XDocument.Parse (s);
XNamespace ns = "http://www.me.com";
int nn = doc.Elements(ns + "ANode").Elemen ts(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.Elemen ts(ns + "ANode").Elemen ts(ns + "BNode").Count( )
or (naming the root explicitly)
doc.Element(ns + "root").Element s(ns + "ANode").Elemen ts(ns +
"BNode").Count( )
--
Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/
Michael Bray wrote:
"Anthony Jones" <An*@yadayadaya da.comwrote in
news:##******** ******@TK2MSFTN GP05.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");
XmlNamespaceMan ager mgr = new XmlNamespaceMan ager(new
NameTable());
mgr.AddNamespac e("pf", "http://www.me.com");
Console.WriteLi ne(root.XPathSe lectElements("p f:ANode/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>Goodby e</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/
"Anthony Jones" <An*@yadayadaya da.comwrote in
news:##******** ******@TK2MSFTN GP05.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'><AN ode><BNode>Hell o</BNode>
<BNode>Goodby e</BNode></ANode></root>";
XmlReader rdr = XmlReader.Creat e(new StringReader(s) );
XDocument doc = XDocument.Load( rdr);
XmlNamespaceMan ager nsm = new XmlNamespaceMan ager(rdr.NameTa ble);
nsm.AddNamespac e("t", "http://www.me.com");
XNamespace ns = "http://www.me.com";
int n = doc.XPathSelect Elements("/t:root/t:ANode/t:BNode", nsm).Count();
int nn = doc.Elements(ns + "root").Element s(ns + "ANode").Elemen ts(ns +
"BNode").Count( );
int nnn = doc.Elements("r oot").Elements( "ANode").Elemen ts("BNode").Cou nt();
var q = doc.XPathSelect Elements("/t:root/t:ANode/t:BNode", nsm);
foreach (var v in q) Console.WriteLi ne(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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 XPath expression to select the namespace nodes with
prefix "foo"? And what's the XPath expression to select the
|
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. The file uses xml namespaces, of which I know very little. But it does seem clear that I can't select the <ss:Worksheet> element by name only; the xpath "//Worksheet" does not match any nodes
Extract from the file, in case this is needed to come...
|
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" and "ns0:Company" but I do not
want to delete their child nodes("w:p","w:r","w:t"). How can I do this?
<ns0:Proposal>
<ns0:Company>
<w:p>
<w:r>
|
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 href="http://www.blogger.com/atom/5281182/108809850419059234"
rel="service.edit" title="test" type="application/x.atom+xml"/>
<author>
<name>Lawrence</name>
</author>
<issued>2004-06-24T19:34:04+02:00</issued>
|
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"/>
<author id='3'>Neward</author>
<author id='2'>Albahari</author>
</book>
| |
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 nodes. The TreeView
populates dynamically as there are a significant number of nodes. The
DataGrid displays both the items and the nodes from the TreeView. Using the
explorer analogy this means the TreeView shows folders, and the DataGrid
folders...
|
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 sure this would work. I think it would be easier if
there was some was you could subclass the XElement, provide your own
namespace and then global search and replace would be much easier.
Then you could do this:
|
by: egholm |
last post by:
I'm using the following XML:
-----------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Url="_catalogs/masterpage" Path="PageLayouts">
<File Url="List.aspx" Type="GhostableInLibrary">
</Module>
</Elements>
------------------------------------------------------------------------------------------
|
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
Sandro
XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
XElement elRoot = new XElement(ns + "urlset",
new XElement ("url",
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |