VB2010如何读取XML文件内容

palmfuns 2018-12-04 06:49:06
有个XML文件,格式如下

<?xml version="1.0" encoding="GBK"?>
<root>
<body>
<vehispara>
<Data Index="1">48.1,9,0.1,0,21.9,60.1,100.0,1</Data>
<Data Index="2">48.1,10,0.1,0,21.9,60.1,100.0,1</Data>
<Data Index="3">48.1,12,0.2,0,21.9,60.1,100.0,1</Data>
<Data Index="4">48.1,10,0.1,0,21.9,60.1,100.0,1</Data>
</vehispara>
</body>
</root>
请问用VB2010如何读取 48.1,9,0.1,0,21.9,60.1,100.0,1 、48.1,10,0.1,0,21.9,60.1,100.0,1 、48.1,12,0.2,0,21.9,60.1,100.0,1 、48.1,10,0.1,0,21.9,60.1,100.0,1 这些字符串。新手第一次接触xml,谢了。
...全文
172 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
寒月孤星love 2018-12-07
  • 打赏
  • 举报
回复
引用 4 楼 寒月孤星love 的回复:


代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml; ///这个必须加

namespace XMLGETEST
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
XmlDocument doc = new XmlDocument();
doc.Load("D:\\C#\\XMLTEST.xml");//文件路径

XmlElement root = null;
root = doc.DocumentElement;

XmlNodeList listNodes = null;
listNodes = root.SelectNodes("/root/body/vehispara/Data");

foreach (XmlNode node in listNodes)
{
listBox1.Items.Add(node.InnerText.ToString());
}
}
}
}


不好意思这是 VS 能用上就看看不能忽略。。。。。。。
寒月孤星love 2018-12-07
  • 打赏
  • 举报
回复


代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml; ///这个必须加

namespace XMLGETEST
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
XmlDocument doc = new XmlDocument();
doc.Load("D:\\C#\\XMLTEST.xml");//文件路径

XmlElement root = null;
root = doc.DocumentElement;

XmlNodeList listNodes = null;
listNodes = root.SelectNodes("/root/body/vehispara/Data");

foreach (XmlNode node in listNodes)
{
listBox1.Items.Add(node.InnerText.ToString());
}
}
}
}
VB业余爱好者 2018-12-07
  • 打赏
  • 举报
回复
'读取XML文件
'xmlFile为要读取的XML文件名,包含完整的路径
Private Sub readXMl(ByVal xmlFile As String)
Dim xmlDoc As System.Xml.XmlDocument
Dim xmlNode As Xml.XmlNodeReader
Dim xmlElement As String = ""

Try
If My.Computer.FileSystem.FileExists(xmlFile) Then
xmlDoc = New Xml.XmlDocument
xmlDoc.Load(xmlFile)
xmlNode = New Xml.XmlNodeReader(xmlDoc)

While xmlNode.Read
Select Case xmlNode.NodeType
Case Xml.XmlNodeType.Element
xmlElement = xmlNode.Name
Case Xml.XmlNodeType.Text
If xmlElement.Equals("Data") Then
TextBox1.Text = TextBox1.Text & vbCrLf & xmlElement & ":" & xmlNode.Value
End If
End Select
End While
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "提示")
End Try
End Sub
palmfuns 2018-12-05
  • 打赏
  • 举报
回复
急等,谢谢大家。
palmfuns 2018-12-04
  • 打赏
  • 举报
回复
<Data Index="1">,<Data Index="2"> 等的节点元素如何处理才能读取?

16,555

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧