Reading an XML file using VB.Net

Hi Experts,

I'm trying to read an XML file with using VB.Net code.

I've got the following little test XML file:

        <FirstName>Kosie </FirstName>            

I also have the following VB code:

    Dim XMLDoc As New Xml.XmlDocument
    Dim XNode As Xml.XmlNode = Nothing

    XNode= XMLDoc.ChildNodes(0).ChildNodes(0)  'This should be FirstName

My problem is that:
XNode now contains the node  <FirstName>Kosie </FirstName>

If I use XNode.Value, NOTHING is returned.  I expected to get "Kosie".

What is wrong?

PantoffelSlippersOperations ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You should use .InnerText or .InnerXml instead.

PantoffelSlippersOperations ManagerAuthor Commented:

I did try innertext but it returns the values of the node and all of its child nodes!

I'm looking to get the value of the single node only.

Any idea?
Firstly, remember that nodes are not elements.  Rather, an XmlNode can be an element, an attribute, a CDATA section or a TEXT section, etc.

So when you use ChildNodes(0) it will be fine for now, but not if you later add attributes to the other elements.

Try using either of the following:
' First
  Dim FirstName As String
  FirstName = XMLDoc.ChildNodes(0).ChildNodes(0).InnerText
' Alternative
  Dim FirstName As String

Open in new window

OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

PantoffelSlippersOperations ManagerAuthor Commented:
Thanks SteveH_UK,

Both method return the values of the firstname node and child nodes:

Copied this from my immediate window:



Any other ideas?

This looks to me like the file is not loading correctly.  I'm surprised it's working at all!

Try doing a dump of XmlDoc.OuterXml.  If you are using Visual Studio (Express) then put a breakpoint at this line and use F5 to launch debug.  Then look at what is shown in the Locals window.  You can then inspect the content of XmlDoc directly to see what different results offer.  You can also use the Watch window to see what XmlDoc.ChildNodes(0) gives, for example.
PantoffelSlippersOperations ManagerAuthor Commented:
OuterXML looks this:



Anything else?
The XML above does not correspond with the XML you initially posted with this question.  Therefore, you are getting a different result.

Why have you embedded the "Initials" tag inside the "FirstName" tag.  Pull it out and this will solve the last query.

The e-mail issue is another question, but make sure your input XML is correct.
PantoffelSlippersOperations ManagerAuthor Commented:

I added the Initials node to illustrate that the InnerText method on FirstName node does not only return the contents of the FirstName node but also the childnode's (Initials) contents.

On my initial post, you replied:
>>>You should use .InnerText or .InnerXml instead.


Then I replied:
>>>>I did try innertext but it returns the values of the node and all of its child nodes!

I added the Initials node to illustrate this.
Ok.  But because you didn't indicate that there was any inner nodes, I was answering a different question.

In this case you should append text() to the XPath, see

  Dim FirstName As String

Open in new window

The text is actually a node, specifically a Text node, so you have to select the Text node within the containing Element node.  This is what the text() XPath construct does.  Note the similarity with attributes which are also XML nodes.
PantoffelSlippersOperations ManagerAuthor Commented:
Sorry SteveH_UK,

I don't follow that last statement.  Could you please elaborate on that?


Let's consider your XML.  I've repeated it below, so we have the same XML in mind.  I've also broken it down into the XmlNodes that represent it:
<RootNode>                                       XmlNode [Element]
  <PersonalDetails>                               XmlNode [Element]
        <FirstName>                                XmlNode [Element]
             Kosie                                  XmlNode [Text]
        </FirstName>                               ------- 
        <LastName>                                 XmlNode [Element]
             Venter                                 XmlNode [Text]
        </LastName>                                -------
        <CellPhone>                                XmlNode [CellPhone]
             082-416-8703                           XmlNode [Text]
        </CellPhone>                               -------
        <EMail>                                    XmlNode [Element]
                      XmlNode [Text]
        </EMail>                                   -------
  </PersonalDetails>                              -------
</RootNode>                                      -------

Open in new window

See for info on the various node types.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PantoffelSlippersOperations ManagerAuthor Commented:

I'll have a look right away
PantoffelSlippersOperations ManagerAuthor Commented:
Thanks SteveH_UK.   After reading through the content, I know more about nodes but I still don't know how to get the text out of a XML node with VB code...
Thanks for your effort - you guys have a tough job with newbies like me.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.