473,407 Members | 2,314 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,407 software developers and data experts.

Searching for nodes


Hi,

I have the following XML file:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<servers xmlns="Servers.xsd">

<server>
<name>my_server_1</name>
<portNumber>119</portNumber>
<description>n/a</description>
<lastAccessed>May 31, 2004</lastAccessed>
<messageCount>0</messageCount>
<user>
<name />
<organization />
<email />
</user>
<forums>
<forum>
<name />
<subscribed />
</forum>
<forum>
<name />
<subscribed />
</forum>
<forum>
<name />
<subscribed />
</forum>
</forums>
</server>
<server>
<name>my_server_2</name>
<portNumber>119</portNumber>
<description>n/a</description>
<lastAccessed>n/a</lastAccessed>
<messageCount>0</messageCount>
<user>

I would like to retrieve the text of the element marked in red. I tried the following code, but I get an error message saying that "Object reference not set to an instance of an object". I suspect that the XPath syntax I am using is incorrect. Any idea on what is wrong? I am new to XML...

XmlDocument doc = new XmlDocument();
doc.Load(fileName);

//Select and display the value of all the ISBN attributes.
XmlElement root = doc.DocumentElement;
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("servers", "Servers.xsd");

// Specific node value.
XmlNode node = root.SelectSingleNode("servers:server/servers:name[text()='my_server_1']/servers:lastAccessed", ns);
Console.WriteLine(node.InnerText);
Thanks.
Mike
Nov 16 '05 #1
6 1233
Yes, the XPath is wrong; try:
servers:server[servers:name='myserver_1']/servers:lastAccessed

"Mike" <no****@nospam.com> wrote in message news:Oe**************@TK2MSFTNGP12.phx.gbl...

Hi,

I have the following XML file:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<servers xmlns="Servers.xsd">

<server>
<name>my_server_1</name>
<portNumber>119</portNumber>
<description>n/a</description>
<lastAccessed>May 31, 2004</lastAccessed>
<messageCount>0</messageCount>
<user>
<name />
<organization />
<email />
</user>
<forums>
<forum>
<name />
<subscribed />
</forum>
<forum>
<name />
<subscribed />
</forum>
<forum>
<name />
<subscribed />
</forum>
</forums>
</server>
<server>
<name>my_server_2</name>
<portNumber>119</portNumber>
<description>n/a</description>
<lastAccessed>n/a</lastAccessed>
<messageCount>0</messageCount>
<user>

I would like to retrieve the text of the element marked in red. I tried the following code, but I get an error message saying that "Object reference not set to an instance of an object". I suspect that the XPath syntax I am using is incorrect. Any idea on what is wrong? I am new to XML...

XmlDocument doc = new XmlDocument();
doc.Load(fileName);

//Select and display the value of all the ISBN attributes.
XmlElement root = doc.DocumentElement;
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("servers", "Servers.xsd");

// Specific node value.
XmlNode node = root.SelectSingleNode("servers:server/servers:name[text()='my_server_1']/servers:lastAccessed", ns);
Console.WriteLine(node.InnerText);
Thanks.
Mike
Nov 16 '05 #2
Dennis Myrén wrote:
Yes, the XPath is wrong; try:

servers:server[servers:name='myserver_1']/servers:lastAccessed
Actually, that doesn't look right - there's no reference to the root
'servers' node.

Try:

servers:servers/servers:server[servers:name='myserver_1']/servers:lastAccessed

or:

servers:server[servers:name='myserver_1']/servers:lastAccessed

In this situation I normally turn to Aaron Skonnard's XPath Expression
Builder, you'll find it here:

http://skonnard.com/articles/170.aspx


"Mike" <no****@nospam.com <mailto:no****@nospam.com>> wrote in
message news:Oe**************@TK2MSFTNGP12.phx.gbl...

Hi,

I have the following XML file:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<servers xmlns="Servers.xsd">

<server>
<name>my_server_1</name>
<portNumber>119</portNumber>
<description>n/a</description>
* <lastAccessed>May 31, 2004</lastAccessed>
* <messageCount>0</messageCount>
<user>
<name />
<organization />
<email />
</user>
<forums>
<forum>
<name />
<subscribed />
</forum>
<forum>
<name />
<subscribed />
</forum>
<forum>
<name />
<subscribed />
</forum>
</forums>
</server>
<server>
<name>my_server_2</name>
<portNumber>119</portNumber>
<description>n/a</description>
<lastAccessed>n/a</lastAccessed>
<messageCount>0</messageCount>
<user>

I would like to retrieve the text of the element marked in red. I
tried the following code, but I get an error message saying that
"Object reference not set to an instance of an object". I suspect
that the XPath syntax I am using is incorrect. Any idea on what is
wrong? I am new to XML...

XmlDocument doc = new XmlDocument();
doc.Load(fileName);

//Select and display the value of all the ISBN attributes.
XmlElement root = doc.DocumentElement;
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("servers", "Servers.xsd");

// Specific node value.
XmlNode node =
root.SelectSingleNode("servers:server/servers:name[text()='my_server_1']/servers:lastAccessed",
ns);
Console.WriteLine(node.InnerText);

Thanks.
Mike

--

Ed Courtenay
[MCP, MCSD]
http://www.edcourtenay.co.uk
Nov 16 '05 #3
Look a little closer.
Mike is already standing at the root element, hence
servers:servers/servers:server[servers:name='myserver_1']/servers:lastAccess
ed
is incorrect, and once again
servers:server[servers:name='myserver_1']/servers:lastAccessed
is correct.

"Ed Courtenay" <re*****************************@edcourtenay.co.uk > wrote in
message news:eN**************@TK2MSFTNGP10.phx.gbl...
Dennis Myrén wrote:
Yes, the XPath is wrong; try:

servers:server[servers:name='myserver_1']/servers:lastAccessed
Actually, that doesn't look right - there's no reference to the root
'servers' node.

Try:

servers:servers/servers:server[servers:name='myserver_1']/servers:lastAccess
ed
or:

servers:server[servers:name='myserver_1']/servers:lastAccessed

In this situation I normally turn to Aaron Skonnard's XPath Expression
Builder, you'll find it here:

http://skonnard.com/articles/170.aspx


"Mike" <no****@nospam.com <mailto:no****@nospam.com>> wrote in
message news:Oe**************@TK2MSFTNGP12.phx.gbl...

Hi,

I have the following XML file:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<servers xmlns="Servers.xsd">

<server>
<name>my_server_1</name>
<portNumber>119</portNumber>
<description>n/a</description>
* <lastAccessed>May 31, 2004</lastAccessed>
* <messageCount>0</messageCount>
<user>
<name />
<organization />
<email />
</user>
<forums>
<forum>
<name />
<subscribed />
</forum>
<forum>
<name />
<subscribed />
</forum>
<forum>
<name />
<subscribed />
</forum>
</forums>
</server>
<server>
<name>my_server_2</name>
<portNumber>119</portNumber>
<description>n/a</description>
<lastAccessed>n/a</lastAccessed>
<messageCount>0</messageCount>
<user>

I would like to retrieve the text of the element marked in red. I
tried the following code, but I get an error message saying that
"Object reference not set to an instance of an object". I suspect
that the XPath syntax I am using is incorrect. Any idea on what is
wrong? I am new to XML...

XmlDocument doc = new XmlDocument();
doc.Load(fileName);

//Select and display the value of all the ISBN attributes.
XmlElement root = doc.DocumentElement;
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("servers", "Servers.xsd");

// Specific node value.
XmlNode node =
root.SelectSingleNode("servers:server/servers:name[text()='my_server_1']/ser
vers:lastAccessed", ns);
Console.WriteLine(node.InnerText);

Thanks.
Mike

--

Ed Courtenay
[MCP, MCSD]
http://www.edcourtenay.co.uk

Nov 16 '05 #4
Dennis Myrén wrote:
Look a little closer.
Mike is already standing at the root element, hence
servers:servers/servers:server[servers:name='myserver_1']/servers:lastAccess
ed
is incorrect, and once again
servers:server[servers:name='myserver_1']/servers:lastAccessed
is correct.

Yep, another case of sending before reading the OP properly! D'oh!
"Ed Courtenay" <re*****************************@edcourtenay.co.uk > wrote in
message news:eN**************@TK2MSFTNGP10.phx.gbl...
Dennis Myrén wrote:
Yes, the XPath is wrong; try:

servers:server[servers:name='myserver_1']/servers:lastAccessed


Actually, that doesn't look right - there's no reference to the root
'servers' node.

Try:


servers:servers/servers:server[servers:name='myserver_1']/servers:lastAccess
ed
or:

servers:server[servers:name='myserver_1']/servers:lastAccessed

In this situation I normally turn to Aaron Skonnard's XPath Expression
Builder, you'll find it here:

http://skonnard.com/articles/170.aspx
"Mike" <no****@nospam.com <mailto:no****@nospam.com>> wrote in
message news:Oe**************@TK2MSFTNGP12.phx.gbl...

Hi,

I have the following XML file:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<servers xmlns="Servers.xsd">

<server>
<name>my_server_1</name>
<portNumber>119</portNumber>
<description>n/a</description>
* <lastAccessed>May 31, 2004</lastAccessed>
* <messageCount>0</messageCount>
<user>
<name />
<organization />
<email />
</user>
<forums>
<forum>
<name />
<subscribed />
</forum>
<forum>
<name />
<subscribed />
</forum>
<forum>
<name />
<subscribed />
</forum>
</forums>
</server>
<server>
<name>my_server_2</name>
<portNumber>119</portNumber>
<description>n/a</description>
<lastAccessed>n/a</lastAccessed>
<messageCount>0</messageCount>
<user>

I would like to retrieve the text of the element marked in red. I
tried the following code, but I get an error message saying that
"Object reference not set to an instance of an object". I suspect
that the XPath syntax I am using is incorrect. Any idea on what is
wrong? I am new to XML...

XmlDocument doc = new XmlDocument();
doc.Load(fileName);

//Select and display the value of all the ISBN attributes.
XmlElement root = doc.DocumentElement;
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("servers", "Servers.xsd");

// Specific node value.
XmlNode node =

root.SelectSingleNode("servers:server/servers:name[text()='my_server_1']/ser
vers:lastAccessed",
ns);
Console.WriteLine(node.InnerText);

Thanks.
Mike

--

Ed Courtenay
[MCP, MCSD]
http://www.edcourtenay.co.uk


--

Ed Courtenay
[MCP, MCSD]
http://www.edcourtenay.co.uk
Nov 16 '05 #5
No worries!
"Ed Courtenay" <re*****************************@edcourtenay.co.uk > wrote in
message news:eW**************@TK2MSFTNGP12.phx.gbl...
Dennis Myrén wrote:
Look a little closer.
Mike is already standing at the root element, hence
servers:servers/servers:server[servers:name='myserver_1']/servers:lastAccess ed
is incorrect, and once again
servers:server[servers:name='myserver_1']/servers:lastAccessed
is correct.


Yep, another case of sending before reading the OP properly! D'oh!
"Ed Courtenay" <re*****************************@edcourtenay.co.uk > wrote in message news:eN**************@TK2MSFTNGP10.phx.gbl...
Dennis Myrén wrote:

Yes, the XPath is wrong; try:

servers:server[servers:name='myserver_1']/servers:lastAccessed

Actually, that doesn't look right - there's no reference to the root
'servers' node.

Try:


servers:servers/servers:server[servers:name='myserver_1']/servers:lastAccess ed
or:

servers:server[servers:name='myserver_1']/servers:lastAccessed

In this situation I normally turn to Aaron Skonnard's XPath Expression
Builder, you'll find it here:

http://skonnard.com/articles/170.aspx

"Mike" <no****@nospam.com <mailto:no****@nospam.com>> wrote in
message news:Oe**************@TK2MSFTNGP12.phx.gbl...

Hi,

I have the following XML file:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<servers xmlns="Servers.xsd">

<server>
<name>my_server_1</name>
<portNumber>119</portNumber>
<description>n/a</description>
* <lastAccessed>May 31, 2004</lastAccessed>
* <messageCount>0</messageCount>
<user>
<name />
<organization />
<email />
</user>
<forums>
<forum>
<name />
<subscribed />
</forum>
<forum>
<name />
<subscribed />
</forum>
<forum>
<name />
<subscribed />
</forum>
</forums>
</server>
<server>
<name>my_server_2</name>
<portNumber>119</portNumber>
<description>n/a</description>
<lastAccessed>n/a</lastAccessed>
<messageCount>0</messageCount>
<user>

I would like to retrieve the text of the element marked in red. I
tried the following code, but I get an error message saying that
"Object reference not set to an instance of an object". I suspect
that the XPath syntax I am using is incorrect. Any idea on what is
wrong? I am new to XML...

XmlDocument doc = new XmlDocument();
doc.Load(fileName);

//Select and display the value of all the ISBN attributes.
XmlElement root = doc.DocumentElement;
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable); ns.AddNamespace("servers", "Servers.xsd");

// Specific node value.
XmlNode node =


root.SelectSingleNode("servers:server/servers:name[text()='my_server_1']/ser vers:lastAccessed",
ns);
Console.WriteLine(node.InnerText);

Thanks.
Mike


--

Ed Courtenay
[MCP, MCSD]
http://www.edcourtenay.co.uk


--

Ed Courtenay
[MCP, MCSD]
http://www.edcourtenay.co.uk

Nov 16 '05 #6

Thanks for all your help!
"Dennis Myrén" <de****@oslokb.no> wrote in message news:3c******************@news2.e.nsc.no...
No worries!
"Ed Courtenay" <re*****************************@edcourtenay.co.uk > wrote in
message news:eW**************@TK2MSFTNGP12.phx.gbl...
Dennis Myrén wrote:
Look a little closer.
Mike is already standing at the root element, hence
servers:servers/servers:server[servers:name='myserver_1']/servers:lastAccess ed
is incorrect, and once again
servers:server[servers:name='myserver_1']/servers:lastAccessed
is correct.


Yep, another case of sending before reading the OP properly! D'oh!
"Ed Courtenay" <re*****************************@edcourtenay.co.uk > wrote in message news:eN**************@TK2MSFTNGP10.phx.gbl...
Dennis Myrén wrote:

Yes, the XPath is wrong; try:

servers:server[servers:name='myserver_1']/servers:lastAccessed

Actually, that doesn't look right - there's no reference to the root
'servers' node.

Try:


servers:servers/servers:server[servers:name='myserver_1']/servers:lastAccess ed
or:

servers:server[servers:name='myserver_1']/servers:lastAccessed

In this situation I normally turn to Aaron Skonnard's XPath Expression
Builder, you'll find it here:

http://skonnard.com/articles/170.aspx

"Mike" <no****@nospam.com <mailto:no****@nospam.com>> wrote in
message news:Oe**************@TK2MSFTNGP12.phx.gbl...

Hi,

I have the following XML file:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<servers xmlns="Servers.xsd">

<server>
<name>my_server_1</name>
<portNumber>119</portNumber>
<description>n/a</description>
* <lastAccessed>May 31, 2004</lastAccessed>
* <messageCount>0</messageCount>
<user>
<name />
<organization />
<email />
</user>
<forums>
<forum>
<name />
<subscribed />
</forum>
<forum>
<name />
<subscribed />
</forum>
<forum>
<name />
<subscribed />
</forum>
</forums>
</server>
<server>
<name>my_server_2</name>
<portNumber>119</portNumber>
<description>n/a</description>
<lastAccessed>n/a</lastAccessed>
<messageCount>0</messageCount>
<user>

I would like to retrieve the text of the element marked in red. I
tried the following code, but I get an error message saying that
"Object reference not set to an instance of an object". I suspect
that the XPath syntax I am using is incorrect. Any idea on what is
wrong? I am new to XML...

XmlDocument doc = new XmlDocument();
doc.Load(fileName);

//Select and display the value of all the ISBN attributes.
XmlElement root = doc.DocumentElement;
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable); ns.AddNamespace("servers", "Servers.xsd");

// Specific node value.
XmlNode node =


root.SelectSingleNode("servers:server/servers:name[text()='my_server_1']/ser vers:lastAccessed",
ns);
Console.WriteLine(node.InnerText);

Thanks.
Mike


--

Ed Courtenay
[MCP, MCSD]
http://www.edcourtenay.co.uk


--

Ed Courtenay
[MCP, MCSD]
http://www.edcourtenay.co.uk

Nov 16 '05 #7

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

Similar topics

1
by: helpful sql | last post by:
Hi all, Following is a sample code from my xml file. <w:body> <ns0:Mpi> <ns0:User> <ns0:Address1> </ns0:Address1> </ns0:User>
1
by: smita | last post by:
Hi, I want to search an xml file for particular searchstrings and also based on the date i.e. all items containing the date -----prior to the specified date, or ----on that date or ----- after the...
2
by: Greg | last post by:
Hi. I have a rather large xml document (object) that can have one or more nodes with a certain attribute throughout (at ANY depth, not at the same level necessarily). I need to find this...
3
by: LC | last post by:
Hello all, Ive been using the treeview and for testing purpose i have created 2 Parent Nodes and 5 child nodes within each parent node i.e.- CN = Child Node Parent Node - CN - CN - CN
3
by: juvi | last post by:
Hi, I have got a problem with Treeview.Nodes.Clear() under VB2005. When I have some nodes in my treeview and a force to clear() all nodes then it seems to work, because the nodes are not visible....
1
by: Daniel Rucareanu | last post by:
Hello, Does anybody knows how can you delete, in just one step, not using a loop, a subset of the child nodes of a given DOM parent node? The subset will be continous, so for example, if the...
15
by: Gigs_ | last post by:
Hi all! I have text file (english-croatian dictionary) with words in it in alphabetical order. This file contains 179999 words in this format: english word: croatian word I want to make...
1
by: j_depp_99 | last post by:
I would like to know what would be the best way to count the nodes accessed while searching for an item in a binary search tree. I have to keep a tally for each item I search for. I have included...
10
by: John Rogers | last post by:
This code only counts the parent nodes or rootnodes in a treeview, how do you count all the nodes in a treeview? // one way int NodeCounter = 0; foreach (TreeNode currentNode in...
1
Sl1ver
by: Sl1ver | last post by:
I've implemented search for my nodes if found it will highlight the node yellow but the nodes is programed to show information about it after its selected how will i achieve the information to be...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
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,...
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...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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...
0
agi2029
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,...
0
isladogs
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...

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.