VB.NET:XPath 问题

发布于 2024-09-17 06:55:32 字数 881 浏览 5 评论 0原文

我有以下 XML:

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

和代码:

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

我想进入第一个迭代“automática”,然后进入最后一个迭代“diária”。怎么了?我该如何解决这个问题?谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猥︴琐丶欲为 2024-09-24 06:55:32

尝试

/configuracoes/gerais/atualizacoes/*

节点的Value始终是所有后代文本节点的串联值。这类似于 HTML,其中 的值为

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

“这是一些粗体文本。”

如果您需要单独的值,请明确选择单独的节点。就您而言,由于它们具有不同的名称,因此我使用了 *

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 *.

故人的歌 2024-09-24 06:55:32

前进的另一个考虑因素是使用 LINQ to XML 而不是 XPath。根据我的经验,通过查询使用和导航 XML 比像 XPath 那样迭代它要容易得多。为了供将来参考,请查看下面的链接:

Visual Basic 中的 LINQ to XML 概述:
http://msdn.microsoft.com/en-us/library/bb384460。 ASPX

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文