473,387 Members | 3,787 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.

How to get XML data out of an XML file

I am trying to retrieve the Parameters first or second (0, 1 ,2) node from
the following XML file:

<?xml version="1.0" encoding="utf-8" ?>
<Robot xmlns="http://tempuri.org/RobotDefaults.xsd">
<Parameters>
<Name>Decker</Name>
</Parameters>
<Parameters>
<Name>A</Name>
</Parameters>
<Parameters>
<Name>B</Name>
</Parameters>
</Robot>

There will be more data than just a name for each Parameters node. Here is
my code:

Dim node As XmlNode = xmlDocument.SelectSingleNode("/Parameters")
RobotName.Text = node.SelectSingleNode("Name").InnerText

I get the dreaded "Object reference not set to an instance of an object."
The SelectSingleNode is not working because I am too dense to figure out how
to write the syntax for the XPAth. I have read the documentation all morning
and am still having trouble.

I have downloaded really nice software called VisualXPath and tried this
query string from it:
/def:Robot/def:Parameters[1]
This gets me a "System Error"

Can anybody show me how to retrieve the "x" number Parameter node from this
document?

Thanks,
Eric
Nov 12 '05 #1
2 5018
>I am trying to retrieve the Parameters first or second (0, 1 ,2) node from
the following XML file:

<?xml version="1.0" encoding="utf-8" ?>
<Robot xmlns="http://tempuri.org/RobotDefaults.xsd">
<Parameters>
<Name>Decker</Name>
</Parameters>
<Parameters>
<Name>A</Name>
</Parameters>
<Parameters>
<Name>B</Name>
</Parameters>
</Robot>

There will be more data than just a name for each Parameters node. Here is
my code:
Try //Robot/Parameters

--
Victor Hadianto
http://www.synop.com/Products/SauceReader/


Dim node As XmlNode = xmlDocument.SelectSingleNode("/Parameters")
RobotName.Text = node.SelectSingleNode("Name").InnerText

I get the dreaded "Object reference not set to an instance of an object."
The SelectSingleNode is not working because I am too dense to figure out
how
to write the syntax for the XPAth. I have read the documentation all
morning
and am still having trouble.

I have downloaded really nice software called VisualXPath and tried this
query string from it:
/def:Robot/def:Parameters[1]
This gets me a "System Error"

Can anybody show me how to retrieve the "x" number Parameter node from
this
document?

Thanks,
Eric

Nov 12 '05 #2
"Victor Hadianto" <sy***@nospam.nospam> wrote in message news:O0*************@TK2MSFTNGP11.phx.gbl...
"flycast" <fl*****@discussions.microsoft.com> wrote in message news:BA**********************************@microsof t.com...
Dim node As XmlNode = xmlDocument.SelectSingleNode("/Parameters")
RobotName.Text = node.SelectSingleNode("Name").InnerText

I get the dreaded "Object reference not set to an instance of an object."
Check whether node is Nothing, flycast, and you're less likely to feel dreadful.

If ( node Is Nothing ) Then
' Do Something Else.
Else
RobotName.Text = node.SelectSingleNode( "Name").InnerText
End If

I'll point out that the Else block can still produce a NullReferenceException,
if the Name node is not found (see next point).

: : <Robot xmlns="http://tempuri.org/RobotDefaults.xsd">
<Parameters> : :
Try //Robot/Parameters


Also try using the XmlNamespaceManager, adding the default namespace URI
of "http://tempuri.org/RobotDefaults.xsd" to it, and passing that along to Select-
SingleNode( ), like this,

Dim nsMan As XmlNamespaceManager = New XmlNamespaceManager( xmlDocument.NameTable)
nsMan.AddNamespace( "def", "http://tempuri.org/RobotDefaults.xsd")
Dim node As XmlNode = xmlDocument.SelectSingleNode( "/def:Robot/def:Parameters", nsMan)
Can anybody show me how to retrieve the "x" number Parameter node from this
document?


XPath lets you use position( ) = x in a predicate (which can be abbreviated to [x] almost like
a C/C++/C# array index). However in XPath, positions start numbering at 1 (so you can
retrieve the 1st Parameter, the 2nd Parameter, etc.). Ammend the previous code snippet
as follows to fetch the second parameter node,

' Code as before, now retrieving the second Parameter node of Robot.
Dim paramNumber As Integer = 2
Dim node As XmlNode = xmlDocument.SelectNodes( "/def:Robot/def:Parameters[" + CStr( paramNumber) + "]", nsMan)
Derek Harmon
Nov 12 '05 #3

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

Similar topics

0
by: Pieter Edelman | last post by:
Hi all, I'm trying to submit some data using a POST request to a HTTP server with BASIC authentication with python, but I can't get it to work. Since it's driving me completely nuts, so here's...
2
by: Fatih BOY | last post by:
Hi, I want to send a report from a windows application to a web page like 'report.asp' Currently i can send it via post method with a context like local=En&Username=fatih&UserId=45&Firm=none...
7
by: Mark Waser | last post by:
Hi all, I'm trying to post multipart/form-data to a web page but seem to have run into a wall. I'm familiar with RFC 1867 and have done this before (with AOLServer and Tcl) but just can't seem...
0
by: sonu | last post by:
I have following client side code which i have used in my asp.net project SummaryFeatured Resources from the IBM Business Values Solution Center WHITEPAPER : CRM Done Right Improve the...
11
by: mesut demir | last post by:
Hi All, When I create fields (in files) I need assign a data type like char, varchar, money etc. I have some questions about the data types when you create fields in a file. What is the...
4
by: alacrite | last post by:
I have a class that I want to turn its contents into csv file. I want to be able to set the value of the delimiter, the name of the file it gets saved to, the path of that file, and maybe a few...
19
by: Zytan | last post by:
I want multiple instances of the same .exe to run and share the same data. I know they all can access the same file at the same time, no problem, but I'd like to have this data in RAM, which they...
1
by: aemado | last post by:
I am trying to read in several lines, each should have exactly 5 pieces of data. I am using try/catch/throw to determine if the data is in the correct format, and trying to use iss to separate the...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.