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"?>
<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>
LVL 2
PNRTAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi PNRT;

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

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.