如何在 VB.NET 中读取 XML 元素 [英] How to read XML elements in VB.NET

查看:44
本文介绍了如何在 VB.NET 中读取 XML 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的问题,但由于我是 XML 的新手,所以我遇到了一些问题.我有这个 XML 文档:

I have a very simple problem, but since I am new to XML, I face some problems. I have this XML document:

<?xml version="1.0" encoding="utf-8"?>  
<Form_Layout>
  <Location>
    <LocX>100</LocX>
    <LocY>100</LocY>  
  </Location>  
  <Size>  
    <Width>300</Width>  
    <Height>300</Height>  
  </Size>  
</Form_Layout> 

我想要做的是将 LocX、LoxY、Width 和 Height 元素的值读入我对应的变量中.

What I want to do is read the values from the LocX, LoxY, Width, and Height elements into my corresponding variables.

这是我尝试过的:

Dim XmlReader = New XmlNodeReader(xmlDoc)  
While XmlReader.Read  
    Select Case XmlReader.Name.ToString()  
        Case "Location"  
            If XmlReader.??  
        Case "Size"  
            If XmlReader.??
    End Select  
End While  

但是,我不知道如何访问每个子节点.

But, I cannot figure out how to access each child Node.

推荐答案

如果您能够使用 Linq to XML,则可以使用 VB 的 XML 轴属性:

If you're able to use Linq to XML, you can use VB's XML Axis Properties:

Dim root As XElement = XDocument.Load(fileName).Root

Dim LocX = Integer.Parse(root.<Location>.<LocX>.Value)
Dim LocY = Integer.Parse(root.<Location>.<LocY>.Value)

root...Value = CStr(120) 也可以.

这篇关于如何在 VB.NET 中读取 XML 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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