Solved

Reading an XML file using VB.Net

Posted on 2007-12-02
17
7,730 Views
Last Modified: 2012-06-27
Hi Experts,

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

I've got the following little test XML file:

<RootNode>
  <PersonalDetails>
        <FirstName>Kosie </FirstName>            
        <LastName>Venter</LastName>
        <CellPhone>082-416-8703</CellPhone>
        <EMail>kosie@kosieInc.co.za</EMail>
  </PersonalDetails>
</RootNode>

I also have the following VB code:

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

    XMLDoc.Load(Me.txtXMLFile.Text)
    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?

Thanks
0
Comment
Question by:PantoffelSlippers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
17 Comments
 
LVL 19

Expert Comment

by:SteveH_UK
ID: 20391654
You should use .InnerText or .InnerXml instead.

See http://msdn2.microsoft.com/en-us/library/system.xml.xmlnode.value(VS.71).aspx
0
 

Author Comment

by:PantoffelSlippers
ID: 20392048
SteveH_UK,

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?
0
 
LVL 19

Expert Comment

by:SteveH_UK
ID: 20394040
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
  XMLDoc.Load(Me.txtXMLFile.Text)
  Dim FirstName As String
  FirstName = XMLDoc.ChildNodes(0).ChildNodes(0).InnerText
 
' Alternative
  XMLDoc.Load(Me.txtXMLFile.Text)
  Dim FirstName As String
  FirstName=XMLDoc.SelectSingleNode("/RootNode/PersonalDetails/FirstName").InnerText

Open in new window

0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:PantoffelSlippers
ID: 20395790
Thanks SteveH_UK,

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

Copied this from my immediate window:

?xmldoc.SelectSingleNode("/RootNode/PersonalDetails/FirstName").InnerText
"Kosie
            K"



?XMLDoc.ChildNodes(0).ChildNodes(0).InnerText
"Kosie
            KVenter082-416-8703kosie@kosieInc.co.za"


Any other ideas?

Thanks
0
 
LVL 19

Expert Comment

by:SteveH_UK
ID: 20397887
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.
0
 

Author Comment

by:PantoffelSlippers
ID: 20417938
OuterXML looks this:

?xmldoc.OuterXml
"<RootNode><PersonalDetails><FirstName>Kosie
            <Initials>K</Initials></FirstName><LastName>Venter</LastName><CellPhone>082-416-8703</CellPhone><EMail>kosie@kosieInc.co.za</EMail></PersonalDetails></RootNode>"



?RootNode.OuterXml
"<FirstName>Kosie
            <Initials>K</Initials></FirstName>"


Anything else?
0
 
LVL 19

Expert Comment

by:SteveH_UK
ID: 20419323
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.
0
 

Author Comment

by:PantoffelSlippers
ID: 20433321
SteveH_UK,

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.

Thanks

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.
0
 
LVL 19

Expert Comment

by:SteveH_UK
ID: 20433377
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 http://www.w3schools.com/xpath/xpath_examples.asp.


XMLDoc.Load(Me.txtXMLFile.Text)
  Dim FirstName As String
  FirstName=XMLDoc.SelectSingleNode("/RootNode/PersonalDetails/FirstName/text()").InnerText 

Open in new window

0
 
LVL 19

Expert Comment

by:SteveH_UK
ID: 20433380
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.
0
 

Author Comment

by:PantoffelSlippers
ID: 20433490
Sorry SteveH_UK,

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

Thanks
0
 
LVL 19

Expert Comment

by:SteveH_UK
ID: 20433494
Certainly.

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]
             kosie@kosieInc.co.za                   XmlNode [Text]
        </EMail>                                   -------
  </PersonalDetails>                              -------
</RootNode>                                      -------

Open in new window

0
 
LVL 19

Accepted Solution

by:
SteveH_UK earned 250 total points
ID: 20433495
See http://www.w3schools.com/dom/dom_nodetype.asp for info on the various node types.
0
 
LVL 19

Expert Comment

by:SteveH_UK
ID: 20433498
0
 

Author Comment

by:PantoffelSlippers
ID: 20433513
Thanks,

I'll have a look right away
0
 

Author Closing Comment

by:PantoffelSlippers
ID: 31412181
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question