0

I am trying to write a small program to get some data from an XML file which is already being used by another application (NOT MY OWN).

The XML Looks like this ...

?xml version="1.0" encoding="utf-8"?

sunjournal

  rvcmappings default="MISSING"

      rvcmap unitId="2" rvcnum="443" /  

      rvcmap unitId="3" rvcnum="103" /

      rvcmap unitId="5" rvcnum="701" /              

  /rvcmappings

/sunjournal

I am trying to use the below Code in VB to get the "rvcnum" for the UnitId of 5.

Dim doc As XmlDocument = New XmlDocument()
doc.Load("C:\BootDrv\Aloha\RptExport\GLMapping_Master.xml") 
Dim acc As String = doc.SelectSingleNode("sunjournal/rvcmappings/rvcmap[UNitId='5']/rvcnum").InnerText

msgbox(acc)

Can anyway point me in the right direction as I do not receive any Errors in Runtime just doesn't show the any Msgbox Data??

Many Thanks Rob

3
  • There is no attribute called UNitId... it's called unitId. XML is case-sensitive. Also, attributes in XPATH must use the @ symbol... so use [@unitId='5']. Commented Aug 26, 2014 at 11:03
  • Hi There I actually should have Cut n Paste the Code instead of re-writing. It doesn actually say UnitId. I am actually getting an Error which I have found now is Object Ref not set to an instance on the doc Object, any ideas? Commented Aug 26, 2014 at 11:05
  • And please learn to format your questions correctly. To use code, indent each line with 4 spaces at the start. Please see the formatting help Commented Aug 26, 2014 at 11:05

1 Answer 1

1

I believe the syntax you are looking for is:

node = doc.SelectSingleNode("//sunjournal//rvcmappings//rvcmap[@unitId='5']")

This will get you the element. From there you can access the attribute:

node.Attributes("rvcnum").Value

Use these docs for some more examples as you work through your program.

Sign up to request clarification or add additional context in comments.

1 Comment

Glad to help. Enjoy your project!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.