• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 751
  • Last Modified:

VB.Net Read -- xml data from string

Hi Experts.  I'm struggling to retrieve the data from this xml string.   There may be as many as 100 stores and I need to get the name, allowed and location value from each store.   Thanks in advance

<?xml version="1.0" encoding="utf-8"?>
  <response-start />
   <response-end />
1 Solution
Fernando SotoRetiredCommented:

Here is what you are looking for using Linq to XML.

' The XML data as a string
Dim storeData = "<?xml version=""1.0"" encoding=""utf-8""?><xmlresponse><response-start /><data><store><name>London</name><allowed>False</allowed><location>1</location></store><store><name>Paris</name><allowed>True</allowed><location>3</location></store><store><name>NY</name><allowed>False</allowed><location>7</location></store></data><response-end /></xmlresponse>"

' Load the string data as a XML document
Dim xdocStores = XDocument.Parse(storeData)
' Parse the XML document to get needed data
Dim stores = From s in xdocStores.Descendants("store") _
             Select New With _
             { _
                 .Name = s.Element("name").Value, _
                 .Allowed = s.Element("allowed").Value, _
                 .Location = s.Element("location").Value _

For Each store in stores
    Console.WriteLine("Name = {0}  Allowed = {1}  Location = {2}", store.Name, store.Allowed, store.Location)

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now