VB.net XML 解析中的跨节点 [英] Step over Node in VB.net XML parsing

查看:27
本文介绍了VB.net XML 解析中的跨节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 VB.net 的 XML 解析功能

I'm trying to use the XML parsing capabilities of VB.net

这是从 Google 的交通路线 API 返回的一些 XML.

Here is some XML returned from Google's traffic directions API.

我用来获取总距离值的 VB.net 代码是

My VB.net code to get the Total distance value is

returnedDistanceMeters = returnedXML...<route>...<leg>...<distance>...<value>.Value

但它是第一个步骤"节点中的值的捷径",并在我想要的 193108 处给了我 88.如何避免它跳到第一个名为距离"的节点?

But it is "short cutting" to the value in the first "step" node and giving me 88 where I want 193108. How do I avoid it jumping to the first node called "distance" ?

推荐答案

不知道有没有更好的方法.但是每次我必须在 .net 上使用 xml 时,我更喜欢使用 xsd.exe 创建一个类.

I don't know if there is a better way. But every time I have to work with xml on .net I prefer to use xsd.exe to create a class.

http://msdn.microsoft.com/en-us/library/x6c1kb0s(v=vs.110).aspx

将 .vb 文件添加到您的项目后,您可以使用此函数从 xml 文件初始化类:

After adding the .vb file to your project, you can initialize the class from a xml file using this function:

Private Function getTraficFromFile(ByVal path As String) As trafic
    Dim stream As New IO.StreamReader(path)
    Dim ser As New Xml.Serialization.XmlSerializer(GetType(trafic))
    Dim mytrafic As New trafic
    mytrafic = CType(ser.Deserialize(stream), trafic)
    stream.Close()

    Return mytrafic
End Function

您可以像普通属性一样访问 xml 值,在您的情况下是这样的:

You can access the xml values like normal properties, in your case something like:

Dim mytraffic as trafic = getTraficFromFile(path)
MsgBox(mytraffic.route.leg.step(19).distance)

这篇关于VB.net XML 解析中的跨节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆