Carl Roe Carl Roe - 2 days ago 5
Vb.net Question

How to Read Specific Value from XML as String

I am trying to read a specific value from this string which is an XML but I do not know how to read it correctly....

I am converting the GetResponseStream into a string:

Dim reader As New StreamReader(httpWebRequest.GetResponse.GetResponseStream())

Dim streamText As String = reader.ReadToEnd()


Here is the string I am trying to parse, I am trying to get the value of
<size>25600</size>
I do not care for any other values of the string(XML).

<?xml version="1.0" encoding="utf-8"?>
<rsMailboxList xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:xml:rsMailboxList">
<offset>0</offset>
<size>50</size>
<total>165</total>
<rsMailboxes>
<rsMailbox>
<name>abaez</name>
<displayName>Arturo Baez Jr.</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>adominguez</name>
<displayName>Alexander dominguez</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>aeam</name>
<displayName>Anna Eam</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>ahaqq</name>
<displayName>Akbar Haqq</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>alans</name>
<displayName>Ben Sigwart</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>aleblanc</name>
<displayName>Andrew Leblanc</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>alert</name>
<displayName>alert .</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>amahller</name>
<displayName>Alon Mahller</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>amazoncustomerservice</name>
<displayName>Amazon Customer Service</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>amazonsales</name>
<displayName>Amazon Sales</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>amclaughlin</name>
<displayName>Aaron Mclaughlin</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>apacheco</name>
<displayName>Anthony Pacheco</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>ardept</name>
<displayName>Accounts Receivable</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>atanega</name>
<displayName>Alfred Tanega</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>atomlinson</name>
<displayName>Adam Tomlinson</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>avalencia</name>
<displayName>Alene Valencia</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>avasquez</name>
<displayName>Adolfo Vasquez</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>bgoegebuer</name>
<displayName>Brent Goegebuer</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>bjoelson</name>
<displayName>Bryan Joelson</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>bookorders</name>
<displayName>TAP Catalogs</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>bpringle</name>
<displayName>Brian Pringle</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>briand</name>
<displayName>Brian Dozois</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>bsummers</name>
<displayName>Brent Summers</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>bwilliams</name>
<displayName>Brian Williams</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>c24shipping</name>
<displayName>4wheelparts Calgary</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>carlc</name>
<displayName>Chris Carlson</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>cbennett</name>
<displayName>Clyde Bennett</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>cburrell</name>
<displayName>Christie Burrell</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>chargebacks</name>
<displayName>Chargebacks Customer Service</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>circulation</name>
<displayName>Circulation</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>ckenworthy</name>
<displayName>Chris Kenworthy</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>cognos</name>
<displayName>cognos cognos</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>cognosadmin</name>
<displayName>Cognos Admin</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>colmstead</name>
<displayName>Clayton Olmstead</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>comptonups</name>
<displayName>Compton UPS</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>cop</name>
<displayName>Coppel Store #24</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>crivas</name>
<displayName>Carlos Rivas</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>cs1</name>
<displayName>Customer Service1 Customer Service1</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>cs2</name>
<displayName>Customer Service2 Customer Service2</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>cs4</name>
<displayName>KAREN LUEDTKE</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>customerservice</name>
<displayName>4wheelparts Customer Service</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>cwhite</name>
<displayName>Cody White</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>davidt</name>
<displayName>David Trigo</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>dc8returns</name>
<displayName>dc8 returns dc8 returns</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>dc8shipping</name>
<displayName>DC8 Shipping</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>denverreturns</name>
<displayName>Denver Returns</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>denvershipping</name>
<displayName>Shipping DC 9</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>dleguizamon</name>
<displayName>Dan Leguizamon</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>dmurphy</name>
<displayName>Dustin Murphy</displayName>
<size>25600</size>
</rsMailbox>
<rsMailbox>
<name>drochon</name>
<displayName>Devin Rochon</displayName>
<size>25600</size>
</rsMailbox>
</rsMailboxes>
</rsMailboxList>


How would I do this? I have looked a great deal online for the answer. I do not want to read
<size>50</size>
I want to get the value of:
<size>25600</size>
into a string.

Answer

Use LINQ to Xml and XML Axis Properties

Using stream As Stream = httpWebRequest.GetResponse.GetResponseStream()
    Dim doc As XDocument = XDocument.Load(stream)
    Dim allSizes = doc.Root.
                      <rsMailboxes>.First().
                      <rsMailbox>.
                      Select(Function(mail) mail.<size>.Value)

    For Each size As string in allSizes
        ' Use size value
    Next
End Using

Classic way without axis properties

Dim allSizes = doc.Root.
                   Element("rsMailboxes").
                   Elements("rsMailbox").
                   Select(Function(mail) mail.Element("size").Value)
Comments