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
0
flycast (4)
9/28/2004 6:11:02 PM
dotnet.xml 7266 articles. 0 followers. Follow

2 Replies
627 Views

Similar Articles

[PageSpeed] 37

>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 


0
synop (25)
9/28/2004 11:57:25 PM
"Victor Hadianto" <synop@nospam.nospam> wrote in message news:O0BPtbbpEHA.596@TK2MSFTNGP11.phx.gbl...
> "flycast" <flycast@discussions.microsoft.com> wrote in message news:BA3BB3E4-3617-4DD6-8EB6-D3D2FA7576B3@microsoft.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


0
loresayer (274)
9/29/2004 12:38:12 AM
Reply:

Similar Artilces:

help with log file
Got a database in SQL2005 that is basically a download of a daily flat text files and creating tables from them. The log.ldf file is frikkin huge. It's over 146GB and I don't need this, I have storage issues. How do I shorten down this file and keep it small forever? Hi Rocketman Have you read about managing the transaction log in the Books Online? How big is your database? What recovery model are you in? What kinds of backups are you doing, and how frequently? If you are not in SIMPLE recovery, and you don't back up your log, it will continue to grow. Even...

Email not working when called from IE or a directory file
When I right click on a file in a directory (such as a photo) I get a menu that includes 'Send to' and an option to 'Mail recipient'. When I click on that it used to launch an email composition window from Outlook and the file is an attachment. For some reason, that function has stopped working. I checked my Outlook preferences and the box designating Outlook as the default email client is checked. I am using Outlook 2003 SP1 and this problem has just started in the last week. My OS is XP Home 5.1.2600. In addition, if I am using Internet Explorer (IE) and try to email a ...

Getting Data Shapes to Work with Custom Stencils
I imported a picture and saved it as a shape. Then I linked data from a SQL database. I can get the data to show up in the Shape Date, but I can't get a Data Graphic to appear. Note that I can get Data Graphics to work with out of the box shapes in the same drawing - just not the custom picture. Tried in both Visio 2007 and 2010 Beta. What am I doing wrong? Thanks in advance. ...

Folders disappearing after compacting file cabinet
Twice, now, I've lost a good chunk of e-mail I've saved in different folders because I've compacted my file cabinet. Anything with a file folder named with E or beyond gets dumped. A through D only remains. Any ideas as to what's going on? Thanks! Karen C. Did you compact or run ScanPST.EXE? I've never heard of a compact deleting valid data unless, of course, the file was damaged before running the compact. I hate to ask but do you have a backup of your PST from before this happened? "Karen C." <kcunningham @ iglou.com> wrote in message news:...

MS Excel file extension changed through e-mail
MS Excel .xlsx files sent as attachments are received without the x, as a .xls instead. The same is true for MS Word .docx changed to .doc Answered in the Word group. Please don't post the same question in multiple groups. It not only causes responders to waste their time replying to questions that have already been answered but it also makes it more difficult for you to follow up on the replies. If it truly is an issue that involves more than one app either post to the Office group rather than the individual groups or use a newsreader rather than the web interface & learn to ...

How to Arrange Data for Chart
I charted the following data in a clustered column chart. Level 1 Level 2 Level 3 Level 4 FY07 - Total 167 23 20 43 81 FY08 - Total 178 22 40 26 90 These are total department errors segregated by levels for each fiscal year. There are 3 different departments involved. What I did was just total the errors for each FY and each level. Now my boss wants to see how many errors were performed within each level by Dept A, Dept B and Dept C and compare the 2 fiscal years. I hope I’m explaining...

Dump file corrupt
************************************************** ************************ THIS DUMP FILE IS PARTIALLY CORRUPT. KdDebuggerDataBlock is not present or unreadable. ************************************************** ************************ Unable to read PsLoadedModuleList ************************************************** ************************ THIS DUMP FILE IS PARTIALLY CORRUPT. KdDebuggerDataBlock is not present or unreadable. ************************************************** ************************ KdDebuggerData.KernBase < SystemRangeStart Windows XP Kernel Version 2600 ...

Network File lock not detected
We have a workbook that about a dozen people get in and out of all day. I have a problem with one user's excel not always locking the file, or it could be a problem with other users not detecting the file is in use and displaying the in use message, do you want to open a read only or wait. Is there anything I can do in execel to fix that or is it a network issue. Running XP pro, office xp pro, and 2k server. From my experience, it is a network issue. They need to unlock the file because Excel cannot. Fixed it, Windows didn't have sp1 on it. For some reason it fixed the pr...

XSD/XML -> RDB in .Net
Hello all! Are there available samples or code which can create "create table" SQL statements using provided XSD and/or XML? Is it possible to get create table statements from DataSets / DataTables in ..Net? Any ideas how to approach this? ...

How to update contacts in WLM via csv file?
I have nearly 2,000 email addresses in my contacts list. I managed to get WLM to list them, although I do notice some are missing. Now, I want to update them with new/revised contacts through a new cvs file, which I exported from OE. However, the contacts import program just hangs and doesn't take. Do I need to delete the existing list first? If so, how do I do that? I am using WLM v.2009 Build 14.0.8 on Windows 7 Are you logging in to Live services? It will say your profile name under the close X. -- Ron Sommer "Bumper21248" wrote in message news...

pst file might be corrupted
Hi all. I am not able to view my pst file at all via my outlook 2000. I tried to recover it via scanpst but scanpst stopped halfway through the process and said that an error occured without specifying which error. Is there any other way to recover this pst file? Thks in advance. How big is the PST file? "inenewbl" <inenewbl@discussions.microsoft.com> wrote in message news:6FB884E0-7C9F-4064-936F-5065E257BB5B@microsoft.com... > Hi all. I am not able to view my pst file at all via my outlook 2000. I > tried > to recover it via scanpst but scanpst stopped halfway ...

Get $500 to your PAYPAL Account by just one CLICK #2
i have hidden the form of getting $500 to your PAYPAL ACCOUNT....CLICK on the IMAGE which is on the RIGHT SIDE of the website http://tricks-4u.4-all.org/ ...

Users and Asset Data linked on Form
Hello everyone, I have two tables, one a Staff list and the other an Asset list extracted from seperate databases, the information in the tables has absolutely no common data, I have a form with the user name and other bits and pieces on from the staff list and also a drop down list of all asset numbers which I can then select the appropiate asset number from then, that asset number and other details I would like to be associated to that user. I have tried, I have done this in the past about 4 years ago but I cannot remember any of it! Any help would most appreciated Cheers David David, T...

VBA to call file based on date
Hello, I wonder if anyone can help me with a VBA routine to check for the presence of a specific file. The file is saved on a dialy basis in the format "userfile_run_YYYYMMDD.xls" i am seeking help with a vb code that will check if today or any specified days report is generated. i guess excel can use the date format to read the specified location for the file. Thank you in advance for your help. .....Coming from Where I'm from. Dim sFile As String sFile = Dir("C:myPath\userfile_run_" & Format(Date,"yyyymmdd") & ".xls") If s...

How to get newsletter on the web
We currently publish a newsletter that is 16 ~ 20 pages. Our January newsletter was 16 pages and the pub file (pub 2002) is 25mb. I can save it as a PDF file and it shrinks it to 7.5mb. If I save all the pages as jpegs, reduce them to 75dpi and then save as a PDF, then we're down to 3.5mb. Is this my best option? I'd like to be able to get the file size even smaller. I am saving the pages as 8.5 x 11 pages. Should I make them smaller than that? We're just getting started with a website and would like to put up our newsletters, but I'd like to get them small enough so th...

"The image file ___ is valid but ...."
"The image file ________ is valid but is for a machine type other than the current machine." What does this message dialog mean on Win XP x64? It means that the program you're trying to run is 16-bit. There is NO 16-bit support in any 64-bit version of Windows. You'll need to run the application in a virtual machine running some earlier version of Windows. -- Charlie. http://msmvps.com/blogs/russel <johnsuth@nospam.com.au> wrote in message news:c1.2c.3KklK9$0TP@PC1.BIGPOND.COM... > > "The image file ________ is valid but is...

replace invalid xml characters
I know that System.Security.SecurityElement.Escape() will replace invalid xml chars with valid equivalent ... But is there another method to accomplish same that is "closer to home" ? * John A Grandy wrote: > I know that System.Security.SecurityElement.Escape() will replace invalid > xml chars with valid equivalent ... > > But is there another method to accomplish same that is "closer to home" ? > > I usually use the stream editor "sed" for this type of stuff. John A Grandy wrote: > I know that System.Security.SecurityElement.E...

How do I put Excel data into a US map format?
I want to feed Excel data about population and trends into a map format instead of a bar graph or pie chart. Is there a plug-in or some such thing that I can use that works with Excel? Ultimately, I want to have each state depicted by a color code for a range of population or an amount of certain data. I am using Excel 2002 in a Windows XP environment. ...

how do I sum only visible data in a column
I have some rows manually hidden, Please is there a formula I can apply to return the sum of the unhidden data contain in a column? Hi Afolabi, If you don't want to use VBA (create a UDF), you could review the following pages: http://www.jkp-ads.com/Articles/ExcelNames08.htm http://www.jkp-ads.com/Articles/ExcelNames09.htm In any case, there is a fundamental issue with both approaches: since hiding/unhiding rows doesn't trigger any event, such event will have to be forced from time to time, or maybe you can leave with the formulae updating their result in the next recalculation...

print mass Powerpoint certificates from Excel file
I get Excel lists of students that require training certificates. I have a PowerPoint certificate. Is it possible to do something simular to a mail merge? In article <BFBA41AC-26D1-4B30-8C54-425C2E8A50F0@microsoft.com>, Go to Girl wrote: > I get Excel lists of students that require training certificates. I have a > PowerPoint certificate. Is it possible to do something simular to a mail > merge? Not out of the box, but we have a commercial PowerPoint add-in that does exactly this. You can try out a fully functional demo to see if it meets your needs. Mo...

Can Import Email Data Filed from Office XP to Office 2003
My HD failed but I had a backup of my Office XP Pro personal file folder with a lot of emails I needed. The file is about 128MB in size. I put a new HD in and installed Office 2003 Pro. When I go to open the old file Outlook 2003 says that the file "is not a personal folders file". (It was backed up to a CD ROM prior to the crash so the source file should be good.) I really don't want to uninstall Office 2003 and put back Office XP. Any idea why 2003 won't open the file and how to work around that? Many thanks in advance to anyone who knows the solution. Alan. The only pro...

How do I get a checkbox to check in Word?
I have a Word document form with numerous checkboxes. I did some updates to it and now the boxes won't check. What can I do to get them to check and un-check again? Protect the document for forms. -- <>>< ><<> ><<> <>>< ><<> <>>< <>><<> Graham Mayor - Word MVP My web site www.gmayor.com Word MVP web site http://word.mvps.org <>>< ><<> ><<> <>>< ><<> <>>< <>><<> "Danieldh" <Da...

NewBie: trying to get a Username from a console App
I am trying to get username on a console app with MFC support . Here is the code #include "stdafx.h" #include "spy.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // The one and only application object CWinApp theApp; using namespace std; BOOL GetUserName( LPTSTR lpBuffer, // address of name buffer LPDWORD nSize // address of size of name buffer ); int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode ...

Importing External Data
I keep trying to import data from an Access database and I continually get the same error. "ODBC Microsoft Access Driver The text file specification "Inserts" does not exist You cannot import., export or link using this specification." Well the spec does exist, the queries run just fine in Access. The data source are linked text files using the "Inserts" specification, which definitely does exist. Sumpens wrong, but danged if I know what it is. Any ideas? TIA Paul Hammond ...

Data Validation date field
I don't understand why this isn't working. I just want to make sure that they don't put a future date in the polydate field. Is me.polydate > now() an invalid expression? code: _________________________________________________________ Private Sub Form_BeforeInsert(Cancel As Integer) If Me.PolyDate > Now() Then Cancel = True Me.PolyDate.SetFocus MsgBox "Please enter a date that falls prior to today's date" Exit Sub End If End Sub It does not appear to be invalid. It is always helpful to post the error you are getting and if it is a runti...