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
548 Views

Similar Articles

[PageSpeed] 18

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

SQL in Excel data
Hi all, Is there a possibility/way to run an SQL query in an excel data sheet? I have quite some data like the sample below, now i would like to have the sum of spending for each person. Like it is possible in Access. A1 B1 Field1 Field2 Chuck 12,89 Mike 23,09 Jean 9,34 Chuck 30,00 Mike 3,80 Chuck 22,00 Mike 7,23 Jean 10,55 Jean 10,75 Jean 31,45 Chuck 19,99 Result Field1 SumOfField2 Chuck 84,88 Jean 62,09 Mike 34,12 Advice would be appriciated. Cheers, Ludovic Hi You could use a formula like this ...

How insert a file into an email w/o it automatically attaching it
Hi, I am trying to insert a word document file from a folder to an email and it keeps attaching it to the email instead of inserting it inside of the email. Thanks for any assistance! "Gary" <Gary@discussions.microsoft.com> wrote in message news:5D04C381-637A-4C6D-937C-2451B7321977@microsoft.com... > Hi, > > I am trying to insert a word document file from a folder to an email and > it > keeps attaching it to the email instead of inserting it inside of the > email. > > Thanks for any assistance! What version of Outlook? use copy and paste or...

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("/P...

Exporting file names to an excel spreadsheet.
I have a folder containing many many many .pdf files. Is there a way to export the filenames of these .pdf files into an excel spreadsheet? If this is a dumb question, I apologize. I didnt have any luck searching for the answer here. -- Kepf ------------------------------------------------------------------------ Kepf's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=30611 View this thread: http://www.excelforum.com/showthread.php?threadid=547520 I use Jim Cone's fine Add-in called "ListFiles"for this sort of thing. It's available free (a...

Get at messages in queue
Is it possible to get at the undeliverable messages that are held in a queue (eg to check the message body or redirect undeliverable messages). I would like to be able to selectively forward them to an alternate email address or to archive them to a PST etc rather than just deleting them? Thanks Alan ...

Publisher file made in Publisher 2007 won't open in 2007
I produced an 8.5 x 11 brochure two days ago in Publisher 2007. I was able to save, resave and reopen the file many times during the day. When I tried to open the file today, I get the "Publisher cannot open the file" error message. I've tried to open the file on two different machines with Publisher 2007 and get the error message both time. One has XP Pro, the other has Visa Home Basic. It's a 1.6 meg file. I have a smaller publisher file of a bumper sticker (40 kb) which was made a couple of weeks ago which opens with no problem. This is the second time I ha...

Multiple PST Files #2
I am running Outlook 2003 and would like to have Outlook use two different .pst files (one for my work stuff and one for home) on the same computer. Is it possible to do this? If so, can I can I configure one of the pst files to get e-mail from one account, while the other pst file is configured to get mail from the other account? Thanks! -D Yes, you can create multiple PSTs. I keep mine stored in the same file location but with different names. Depending on which version of Outlook you run, you can creat a new pst from file/new/Outlook Data File (I run Outlook 2003 but had multiple P...

Money 2006 file repair application error
I cannot delete an old account because I get a "file may be courrupt" error so I am trying to repair my 2006 file. Following the deirections, when the progress bar almost completes, I get an application exception and send an error report to MS. My event log has 2 app errors 1) Event Type: Error Event Source: Application Error Event Category: None Event ID: 1001 Date: 4/12/2006 Time: 10:47:26 PM User: N/A Computer: xxx Description: Fault bucket 02266399. For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp. Data: 0000: 42 75 63 6b 65 ...

How to get paper size from dmPaperSize returned from GetDevMode?
For example, dmPaperSize returns 1 which is DMPAPER_LETTER, how do I calculate the paper size like in this case 8.5"x11"? There are so many paper sizes, is there any function call to calculate based on the return value of dmPaperSize or mapping between the value of dmPapersize and actual size? Thank you. The reason I asked the question was because dmPaperLength and dmPaperWidth are 0 for some printers. Also, my HP laserjet returns as a color printer (dmColor = 2) from GetDevMode call. Anyone knows why? ...

does vista installed on virtual machine 2007 get wsus updates ?
It is searching for updates but it is not finding anything and saying that Windows is up to date. I have set the updates to install from the wsus server and assigned the updates to the same Vista virtual machine .. Thank you -- aconti ------------------------------------------------------------------------ aconti's Profile: http://forums.techarena.in/members/73272.htm View this thread: http://forums.techarena.in/active-directory/1290161.htm http://forums.techarena.in Hello aconti, If the machine is getting the correct GPO for the WSUS settings, check with rsop...

Application hanging on file load...
Okay, I'm having the strangest problem, and I'm hoping somebody here will be able to give me an idea of how to attack it... I have an MFC application that does some networked communication, multi-threading, fun stuff... I also load and save xml configuration files. Now, on most of the computers I've run my software on, I have absolutely no problems. On SOME boxes, however, whenever I use my menu to try to load or save a file... before the File Dialog box even comes up... the application hangs. All the boxes I've tried this on have been Windows 2000 boxes with Service Pack 4....

can't open files
I have Microsoft Office 2003 ~ all of a sudden when I go to my documents and try to open a file, I get this message "problem sending command to the program". Error 0646 and dt000 See the article "How to re-register Word when problems crop up opening files” at: http://www.word.mvps.org/FAQs/AppErrors/ReRegisterWord.htm -- Hope this helps. Please reply to the newsgroup unless you wish to avail yourself of my services on a paid consulting basis. Doug Robbins - Word MVP, originally posted via msnews.microsoft.com "Lost in Vegas" <Lo...

Baseball Stats question: How can I get the RBI's?
I have a data table that looks like this: Code ------------------- STR-S SNK-S STR-K STR-F SNK-S STR-3 STR-2 STR-F SNK-S SNK-4 SNK-S STR-3 SNK-F STR-4 STR-O ------------------- "STR", etc at the beginning are pitch types, SNK is Sinker for example The end character is the result of the pitch, a 4 would be a home run, 3 a triple, and so forth. O is out, F is foul, S is strike --- you ge it. How can I figure the amount of RBI's? I know that it's 5, but I can' think of any automated way to calculate this, anyone got any ideas? THANK YOU!! :confused -- AVER...

Any XML Naming Conventions?
Does the use of DTD, XML Schema and similar constructs adopt the use of C# naming conventions? If so how do I make the distinction of how to apply C# conventions with XML elements, attributes and so on? Any referrals to resources that discuss or document XML Naming Conventions? -- <%= Clinton Gallagher, "Twice the Results -- Half the Cost" Architectural & e-Business Consulting -- Software Development NET csgallagher@REMOVETHISTEXTmetromilwaukee.com URL http://www.metromilwaukee.com/clintongallagher/ clintonG wrote: > Does the use of DTD, X...

Locking cell color while allowing data changes in cell
In excel 2000, I created an attendance worksheet for my classes.(Alphabetized names down left vertical column. Dates across top of horizontal row.) I added a different color to all cells in every other row to make for easier reading of each student's name and absences. Every other row stays with a white background. My question: I wondered if it was possible to lock row colors while allowing data to change on top of them. If a new student is added to my class in alphabetical order, the alternating color pattern is often lost. It is a pain to rechange row and cell colors. Any shortcut ...

How refresh imported data automatically?
In Excel, I've imported data into a worksheet. I know I can click Data.. Refresh Data to requery the source, but I want to be able to do it in a more automated way. Is there some sort of macro I can write when opened or something? Thanks, Ron Ron, Select the cell the data starts in, select Data/Import External Data/Data Range Properties Check the box "Refresh on file open" and anything else that needs checking off... Other than that, you could record a new Macro that selects your cell, refreshes data, and assign the macro to a keystroke, or even an icon. Beege ...

I can no longer convert my files to pdf
I have Publisher 2003 and yesterday, my pdf menu on the top left disappeared and I can no longer convert my Publisher files to pdf. I've even uninstalled/reinstalled the Adobe Acrobat 7 and have also updated to 7.8 but still no luck. The next step was to uninstall and reinstall Publisher and still no luck. Any one know what may be the problem? Thanks. How are you trying to convert your files? When happens when you try? -- JoAnn Paules MVP Microsoft [Publisher] "darrellm" <darrellm@discussions.microsoft.com> wrote in message news:A78B7B52-981D-4FB7-8625-EAD9...

Question About Missing Data
So, this is probably really easy, but I just want to ask and see if I may be missing something here. Some data on vendor numbers changed. Let’s say IBM used to have a vendor number 12345 and now it’s vendor number is 56789. I can identify IBM as IBM, but I really want to use the number, not the name. Should I set up a table that ties the numbers together, so that Access knows 12345 = 56789? Or, should I do some kind of Update Query and change all incidences of 12345 to be 56789? Or, is there some other, method, like a ‘best practices for missing data’? Thanks! Ryan--- ...

Outlook Data Files #4
I've done some reading but I'm still confused about the use of Office Outlook Personal Folders File (.pst) versus Outlook 97-2002 Personal Folders File (.pst). I understand the basic "Office Outlook Personal Folders File (.pst) to create a new Outlook 2003 .pst file. Click Outlook 97-2002 Personal Folders File (.pst) to create a new Outlook .pst file that is compatible with earlier versions of Outlook." When we have upgraded to Outlook 2003 or set up a new PC with Outlook 2003, when adding PST's you can chosse either of the two file type options and browse to chos...

New Named Range Created Each Time Data Imported into Excel via Macro
I have noticed that each time I import data into an Excel spreadsheet via a macro, a new named range (for the same range) is created. This does not pose a problem, but after a while, I'll have a huge number of named ranges that will never be used. Why does Excel name the range and how can I stop this? I noticed in the recorded macro, there was a line .Name = "drd_5". I commented this out to see what would happen, but it just renamed the range "ExternalData_5". The next one was "ExternalData_6", etc. Thanks, Carroll Rinehart You could refresh with differen...

Backup Excel file
I want to take backup of a file every time. I am using - save as - tools - general option - always create back up. The backup file is in the same location but I want to put it in a different folder. What should I do, kindly suggest. Thanks Jan Karel Pieterse's has an addin called AutoSafe (note spelling). http://www.jkp-ads.com/Download.htm (look for AutoSafe.zip, not autosafeVBE.zip, for your purposes.) It doesn't overwrite the existing workbook when it saves. It saves to a user selectable folder. And when it's done, it either deletes these backups (or puts them...

Getting Cell Value from the Concatenate formula
I built a concatenate formula that returns the following result: =Jul! $D27 I am looking for the cell contents of Sheet: July Column D Row 27. I tried to use offset, but I am stumped. Can I add something to the front of the concatenate to not only build the reference to the cell, but also return the value instead of the =Jul!$D27 ? Thanks John =indirect(yourformulahere) Don't include the equal sign in your formula. And match the name correctly (Jul or July???). Depending on the name of the worksheet, you may need to have a string that looks like: 'Sheet 99'!d27 =indirec...

Utility (pref. script) to catalog .EDB files
Hi, I have a client that is using an Enterprise backup tool which, whilst able to back-up and restore, does not provide in-depth information like traditional Exchange-aware backup products. I have been asked to write a script to list the users in a flat .EDB file (which has been snapped and copied and flushed using SAN replication tools). I have taken a look at the ESE API, which can only be accessed using C + (I only use VB). Other than EXMERGE, does anyone know of any tools that allow you to list the basic mailbox details on a EDB file? Thanks, Hutton. ...

Can't get rules to run.
I can't get Outlook 2003 rules to run. I am getting my mail from a pop3 account and would like to have new mail sorted to specific folders when it arrives. I set up the rules as I did in previous versions of outlook, but they do not run automatically. I can run the rules manually and they will move the mail. I have this rule (for example Apply this rule after the message arrive from some@email.address and on this machine only move it to the somefolder folde I have tried deselecting on this machine only Thanks. Do you have an Exchange service in this profile? Are you using Cache...

List folders to file
Hi Does anyone have a macro that would list all the folders (with path) and subfolders to an excel sheet but not the files? Thanks Shona Shona- There's a function called CELL() that should return your filename (or any of a number of parameters) but I can't get it to work properly on my machine, so I can't make it work for you. The syntax is =CELL("filename") which ought to return the path and filename of the current file, and which you could then trim down to show just the path. Can anyone provide some guidance here? Dave, If it isn't working for you, it pro...