views:

54

answers:

2

Dim str as string = "<request id=value1 type=value2>value3</request>"

How could select the values as follows...

Dim id as string = get the value of id (value1)
Dim type as string = get the value of type (value 2)
Dim ReadValue3 as string = get the value3
+3  A: 

Well, I can't see why you'd need to use LINQ itself, but you could certainly use LINQ to XML:

Dim element as XElement = XElement.Parse(str)
Dim id as String = CType(element.Attribute("id"), String)
Dim type as String = CType(element.Attribute("type"), String)
Dim value as String = element.Value

(Apologies if the VB has syntax issues... it's not my mother tongue, so to speak.)

Jon Skeet
Hi John! Pardon me but i am a newbie. So what other options (besides LINQ) do i have? By the way i get this error 'value1' is an unexpected token. The expected token is '"' or '''.
Chocol8
@strakastrokas: Well LINQ itself is about querying over collections - you're not really doing a query over a collection, just doing normal XML stuff. "LINQ to XML" is really just an XML API which works well with LINQ to Objects. You could use the old DOM API (XmlDocument etc) instead, but it would be clunkier. Your XML isn't valid at the moment - you need quotes around the attribute values, which is why you're getting an exception.
Jon Skeet
A: 

Note it is possible to use linq with strings see http://msdn.microsoft.com/en-us/library/bb397915.aspx

you just wouldn't want to when dealing with xml

Roman A. Taycher