views:

23

answers:

2

Hi, I have the follwing XML:

<?xml version="1.0" encoding="utf-8" ?>
<configuracoes>
  <gerais>
    <atualizacoes>
      <tipo>automática</tipo>
      <frequencia>diária</frequencia>
    </atualizacoes>
  </gerais>
</configuracoes>

And the code:

Dim xPathNavigator As XPathNavigator
Dim xPathNodeIterator As XPathNodeIterator

xPathNavigator = Me.XML.CreateNavigator()
xPathNodeIterator = xPathNavigator.Select("/configuracoes/gerais/atualizacoes")

While (xPathNodeIterator.MoveNext())
    Dim xPathNavigatorInterno As XPathNavigator = xPathNodeIterator.Current
    MsgBox(xPathNavigatorInterno.Value) 'It Shows "automáticadiária" instead of "automática" and then "diária" in the next iteration...
End While

I want to get in the first iteration "automática" and then, in the last one "diária". What's wrong? How could I solve that? Thank you.

+2  A: 

Try

/configuracoes/gerais/atualizacoes/*

The Value of a node always is the concatented value of all descendant text nodes. This is analoguous to HTML, where the value of

<div>This is some <b>bold</b> text.</div>

is "This is some bold text.".

If you want the indiviual values, select the individual nodes explicitly. In your case, since they have different names, I used the *.

Tomalak
Yes, you was right. Thank you very much.
thomas
@Tomalak: +1 Good explanation.
Alejandro
A: 

Another consideration moving forward is to use LINQ to XML rather than XPath. In my experience it is much easier to use and navigate XML via queries than iterating through it like XPath does. For future reference take a look at the link below:

Overview of LINQ to XML in Visual Basic:
http://msdn.microsoft.com/en-us/library/bb384460.aspx

atconway
@atconway: I think it's not good practice to answer an XPath tagged questions with a recommendation to use a vendor locked library...
Alejandro
That's fine as your opinion and I have mine. This post is tagged with '.NET' and 'VB.NET' so remember using LINQ in the world and realm of .NET applications would be much preferred in current day to XPath, and could help the OP do things easier. In fact if you are referring to "Vendor Locked" library because it is within the .NET Framework, you can't even create a VB.NET application without already using a dozen of these so called "Vendor Locked" libraries.
atconway