user1206410
user1206410

Reputation: 223

vb.net deserialize an xml string

I've got a simple xml string that looks like this:

<?xml version="1.0"?>
<AccountBalance>
    <value>
        22.00
    </value>
</AccountBalance>

I'd like to set the value of <value> to a variable in vb.net. How do I do this?

Upvotes: 0

Views: 3537

Answers (2)

Tim
Tim

Reputation: 28530

Not sure where serialization comes into play for this, but if it's just a simple XML string you can use LINQ to XML to get the value quite easily:

Dim xml As XElement = New XElement.Parse(xmlString)

Dim Balance As Integer = From x in xml.Descendants("value")
                         Select CInt(x.Value)

This will give you a collection of the value elements in the XML. If you only have one, you can also do this:

Dim Balance As Integer = (From x in xml.Descendants(xmlString)
                         Select  CInt(x.Value)).SingleOrDefault()

xmlString is the XML string you want to get values from - the Parse method loads the xml from a supplied string. Use .Load if it's in a file.

Syntax might be a bit off - I'm doing this off the top of my head.

Upvotes: 1

dreamgt
dreamgt

Reputation: 137

How are you trying to pass the XML as a Stream, TextReader, or XmlReader? Please refer to this XmlSerializer.Deserialize Method

Upvotes: 0

Related Questions