如何在 VB.NET 中读取 XML 元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11581692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:36:04  来源:igfitidea点击:

How to read XML elements in VB.NET

xmlvb.net

提问by Nianios

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

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

<?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> 

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

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

Here is what I have tried:

这是我尝试过的:

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.

但是,我无法弄清楚如何访问每个子节点。

回答by Mark Hurd

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

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

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

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

And root.<Location>.<LocY>.Value = CStr(120)works too.

root.<Location>.<LocY>.Value = CStr(120)有效。

回答by Steven Doggart

Here's how you can do it with XmlDocumentand XPath. I'm sure someone else will be happy to volunteer an example using XDocumentand LINQ.

下面是您如何使用XmlDocumentXPath来完成它。我相信其他人会很乐意自愿提供一个使用XDocumentLINQ的示例。

Dim doc As New XmlDocument()
doc.LoadXml("...")
Dim locX As Integer = Integer.Parse(doc.SelectSingleNode("/FormLayout/Location/LocX").InnerText)
Dim locY As Integer = Integer.Parse(doc.SelectSingleNode("/FormLayout/Location/LocY").InnerText)
Dim width As Integer = Integer.Parse(doc.SelectSingleNode("/FormLayout/Size/Width").InnerText)
Dim height As Integer = Integer.Parse(doc.SelectSingleNode("/FormLayout/Size/Height").InnerText)

Also, you may want to take a look at the XmlSerializerclass, and see if that is something you are interested in. That class will read the XML document and use it to populate the property values of a new object. You just need to create a class that mimics the structure of the XML for it to deserialize into.

此外,您可能想查看XmlSerializer该类,看看您是否对它感兴趣。该类将读取 XML 文档并使用它来填充新对象的属性值。您只需要创建一个类来模拟 XML 的结构,以便将其反序列化。

回答by adatapost

Use LINQ-XML,

使用 LINQ-XML,

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

Dim LocX = Integer.Parse(root.Element("Location").Element("LocX").Value)
Dim LocY = Integer.Parse(root.Element("Location").Element("LocY").Value)