Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the value from a specific node in an XML file?

From this XML code:

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

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.)

like image 992
Yorrick Avatar asked May 23 '12 18:05

Yorrick


People also ask

How do I select a specific node in XML?

To find nodes in an XML file you can use XPath expressions. Method XmlNode. SelectNodes returns a list of nodes selected by the XPath string. Method XmlNode.

How do I get the value of a tag in XML?

var val1 = elem. Element("LOCATION"); var Value1 = val1 !=

What tells where exactly a particular node is located in the XML document?

Location path specifies the location of node in XML document. This path can be absolute or relative. If location path starts with root node or with '/' then it is an absolute path. Following are few of the example locating the elements using absolute path.


2 Answers

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

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):

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:

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
like image 88
Steven Doggart Avatar answered Sep 19 '22 00:09

Steven Doggart


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.

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:

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).

like image 27
psubsee2003 Avatar answered Sep 18 '22 00:09

psubsee2003