如何从XML文件中的特定节点获取值?

时间:2022-11-27 17:19:46

From this XML code:

从这个XML代码:

<?xml version="1.0" encoding="utf-8"?>
<Tabel>
  <Member>
    <Naam>Cruciatum</Naam>
    <Kills>1000</Kills>
    <Deaths>10</Deaths>
    <KD>100</KD>
  </Member>
  <Member>
    <Naam>Ghostbullet93</Naam>
    <Kills>10</Kills>
    <Deaths>1</Deaths>
    <KD>10</KD>
  </Member>
</Tabel>

How can I get (for example) the 10 next to <Kills> ?

我如何获得(例如) 旁边的10?

I've tried multiple things without any success. One of the ideas I had was using this code:

我尝试过多次没有成功的事情。我的一个想法是使用此代码:

Dim doc = XDocument.Load("C:\members.xml")
        Dim members = From m In doc.Element("Tabel").Elements("Member")
                      Select naam = m.Element("Naam").Value
        For Each member In members
            lstmembers.Items.Add(member)
        Next

But I can't figure out how to edit that snippet to work with what I need it to do now.

但我无法弄清楚如何编辑该片段以使用我现在需要它做的事情。

(The above code works perfectly for where it's used.)

(以上代码适用于使用它的位置。)

2 个解决方案

#1


9  

You can also use XPath to read the element's value:

您还可以使用XPath来读取元素的值:

Dim doc As XmlDocument = New XmlDocument()
doc.Load("C:\members.xml")
Dim kills As String = doc.SelectNode("Tabel/Member[Naam='Ghostbullet93']/Kills").InnerText

If, however, you intend to load and use all the data, it would be far easier to use serialization. To do that, you first need to create classes that mimic the XML structure (for simplicity sake I'll just use public string fields, but it would be better to use properties):

但是,如果您打算加载和使用所有数据,那么使用序列化会容易得多。要做到这一点,首先需要创建模仿XML结构的类(为简单起见,我只使用公共字符串字段,但最好使用属性):

Public Class Member
    Public Naam As String
    Public Kills As Integer
    Public Deaths As Integer
    Public KD As Integer
End Class

Public Class Tabel
    <XmlElement("Member")> _
    Public Members As List(Of Member)
End Class

Then deserialize the XML like this:

然后像这样反序列化XML:

Dim serializer As XmlSerializer = New XmlSerializer(GetType(Tabel))
Dim tabel As Tabel = CType(serializer.Deserialize(File.OpenRead("C:\members.xml")), Tabel)
For Each member As Member in tabel
    Dim kills As Integer = member.Kills
Next

#2


2  

XPath or XmlDeserialization a recommended by Steve are excellent options, but for a pure LINQ solution, you just need to add an appropriate Where clause to your query.

Steve推荐的XPath或XmlDeserialization是很好的选择,但对于纯LINQ解决方案,您只需要在查询中添加适当的Where子句。

Dim doc = XDocument.Load("C:\members.xml")
Dim members = From m In doc.Element("Tabel").Elements("Member")
              Where m.Element("Naam").Value = "Ghostbullet93"
              Select kills = m.Element("Kills").Value

members will still be an IEnumerable<String> in this example, so if you only have 1 object, you need to do something like:

在这个例子中,成员仍然是IEnumerable ,所以如果你只有1个对象,你需要做类似的事情:

Dim member = members.First()  // will throw exception if collection is empty

or

要么

Dim member = members.Single()  // will throw exception if collection is empty or has 2 or more elements

(My vb.NET is extremely rusty, so please forgive any syntax errors).

(我的vb.NET非常生疏,所以请原谅任何语法错误)。

#1


9  

You can also use XPath to read the element's value:

您还可以使用XPath来读取元素的值:

Dim doc As XmlDocument = New XmlDocument()
doc.Load("C:\members.xml")
Dim kills As String = doc.SelectNode("Tabel/Member[Naam='Ghostbullet93']/Kills").InnerText

If, however, you intend to load and use all the data, it would be far easier to use serialization. To do that, you first need to create classes that mimic the XML structure (for simplicity sake I'll just use public string fields, but it would be better to use properties):

但是,如果您打算加载和使用所有数据,那么使用序列化会容易得多。要做到这一点,首先需要创建模仿XML结构的类(为简单起见,我只使用公共字符串字段,但最好使用属性):

Public Class Member
    Public Naam As String
    Public Kills As Integer
    Public Deaths As Integer
    Public KD As Integer
End Class

Public Class Tabel
    <XmlElement("Member")> _
    Public Members As List(Of Member)
End Class

Then deserialize the XML like this:

然后像这样反序列化XML:

Dim serializer As XmlSerializer = New XmlSerializer(GetType(Tabel))
Dim tabel As Tabel = CType(serializer.Deserialize(File.OpenRead("C:\members.xml")), Tabel)
For Each member As Member in tabel
    Dim kills As Integer = member.Kills
Next

#2


2  

XPath or XmlDeserialization a recommended by Steve are excellent options, but for a pure LINQ solution, you just need to add an appropriate Where clause to your query.

Steve推荐的XPath或XmlDeserialization是很好的选择,但对于纯LINQ解决方案,您只需要在查询中添加适当的Where子句。

Dim doc = XDocument.Load("C:\members.xml")
Dim members = From m In doc.Element("Tabel").Elements("Member")
              Where m.Element("Naam").Value = "Ghostbullet93"
              Select kills = m.Element("Kills").Value

members will still be an IEnumerable<String> in this example, so if you only have 1 object, you need to do something like:

在这个例子中,成员仍然是IEnumerable ,所以如果你只有1个对象,你需要做类似的事情:

Dim member = members.First()  // will throw exception if collection is empty

or

要么

Dim member = members.Single()  // will throw exception if collection is empty or has 2 or more elements

(My vb.NET is extremely rusty, so please forgive any syntax errors).

(我的vb.NET非常生疏,所以请原谅任何语法错误)。