如何从XML读取特定值作为字符串

时间:2016-12-01 21:43:44

标签: vb.net

我正在尝试从此字符串中读取一个特定值,这是一个XML,但我不知道如何正确读取它....

我正在将GetResponseStream转换为字符串:

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

Dim streamText As String = reader.ReadToEnd()

这是我试图解析的字符串,我试图得到<size>25600</size>的值我不关心字符串的任何其他值(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>

我该怎么做?我在网上看了很多答案。我不想阅读<size>50</size>我希望将<size>25600</size>的值变为字符串。

1 个答案:

答案 0 :(得分:0)

Use LINQ to XmlXML 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

没有轴属性的经典方式

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