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

4

我是一个有用的助手,可以为您翻译技术方面的中文内容。以下是需要翻译的内容:

我遇到了一个非常简单的问题,但由于我刚接触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> 

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

这是我尝试过的方法:

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  

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

1
你正在使用哪个版本的.NET? - Inisheer
4个回答

4

如果你能使用Linq to XML,你可以使用VB的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.<Location>.<LocY>.Value = CStr(120) 也是可行的。


0
每个 XML 文档可能在最顶部位置有一个声明节点。声明节点提供有关 XML 文档的信息:
<?xml version="1.0" encoding="utf-8"?> 

在声明节点之后的 XML 文档中必须存在根节点。在您的情况下,根节点名为“Form_Layout”。
<Form_Layout>

System.Xml命名空间中可用的类对象可用于读取、操作和保存数据到XML文件中。 您可以使用FileStream对象读取XML文件的全部内容。您可以修改节点,然后将其保存到新文件或覆盖现有文件。 以下代码可用于将XML文件加载到XmlDataDocument对象中。
    Dim xmlDoc As System.Xml.XmlDataDocument = New System.Xml.XmlDataDocument
    Dim filepath As String = "C:\Users\mraso\Documents\location.xml"
    Dim loadedIn As Boolean = False, readbln As Boolean = False
    Dim fstream As System.IO.FileStream = New System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
    If fstream IsNot Nothing Then
        xmlDoc.Load(fstream)
        loadedIn = True
    End If
    fstream.Close()
    fstream.Dispose()
    fstream = Nothing

您可以使用 XmlDataDocument 对象的 ChildNodes 属性来读取 xml 文档的声明节点和根节点。您还可以调用每个节点的 ChildNodes 属性以返回其子节点。 您可以迭代 XmlNodeList 对象中的项以访问单个节点。 与您的 xml 文件一起工作的整个代码如下所示。
Private Sub TestReadingAndSavingXml()
    Dim xmlDoc As System.Xml.XmlDataDocument = New System.Xml.XmlDataDocument
    Dim filepath As String = "C:\Users\mraso\Documents\location.xml"
    Dim loadedIn As Boolean = False, readbln As Boolean = False
    Dim fstream As System.IO.FileStream = New System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
    If fstream IsNot Nothing Then
        xmlDoc.Load(fstream)
        loadedIn = True
    End If
    fstream.Close()
    fstream.Dispose()
    fstream = Nothing
    Dim ndList1 As System.Xml.XmlNodeList = xmlDoc.ChildNodes
    If ndList1 IsNot Nothing Then
        Dim cnt As Integer = ndList1.Count
        For Each nd1 As System.Xml.XmlNode In ndList1
            If nd1.Name = "Form_Layout" Then
                Dim nd2 As System.Xml.XmlNode = GetChildNode(nd1, "Location")
                If nd2 IsNot Nothing Then
                    Dim nd3 As System.Xml.XmlNode = GetChildNode(nd2, "LocX")
                    If nd3 IsNot Nothing Then
                        Dim LocX_value As Integer = nd3.InnerText
                        Dim s = LocX_value
                    End If
                End If
            End If
        Next
    End If
    'Save data to a new xml file
    Dim outputXml As String = "C:\Users\mraso\Documents\location2.xml"
    xmlDoc.Save(outputXml)
End Sub

Function GetChildNode(ByRef node As System.Xml.XmlNode,
                      ByVal nodeName As String) As System.Xml.XmlNode
    If node IsNot Nothing Then
        Dim ndList1 As System.Xml.XmlNodeList = node.ChildNodes
        If ndList1 IsNot Nothing Then
            For Each nd1 As System.Xml.XmlNode In ndList1
                If nd1.Name = nodeName Then
                    Return nd1
                End If
            Next
        End If
    End If
    Return Nothing
End Function

上述代码将读取值100并将其放入以下xml文件中的节点“LocX”中。
<?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>

0

以下是使用 XmlDocument 和 XPath 的方法。我相信其他人也会很乐意提供使用 XDocument 和 LINQ 的示例。

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)

此外,您可能想要查看一下XmlSerializer类,并确定它是否符合您的需求。该类将读取XML文档并使用它来填充新对象的属性值。您只需要创建一个模仿XML结构的类,就可以将其反序列化为对象。

非常感谢。 如果有人能给出一个LINQ的例子,我会非常高兴。 - Nianios

0
使用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)

另一个问题是,如果我想要相反的操作怎么办: root.Element("Location").Element("LocX").Value=locX '其中 locX 是我的变量 这样正确吗? - Nianios

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接