Searching for nodes

M

Mike

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
 
?

=?iso-8859-1?Q?Dennis_Myr=E9n?=

Yes, the XPath is wrong; try:
servers:server[servers:name='myserver_1']/servers:lastAccessed


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
 
E

Ed Courtenay

Dennis said:
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

message
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
 
D

Dennis Myrén

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 said:
Dennis said:
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

message
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
 
E

Ed Courtenay

Dennis said:
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!
Dennis said:
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


message
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
 
D

Dennis Myrén

No worries!
Ed Courtenay said:
Dennis said:
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!
message
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



message
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
 
M

Mike

Thanks for all your help!


No worries!
Ed Courtenay said:
Dennis said:
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!
message
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



message
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
 

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

Top