简体   繁体   中英

VB.NET Read all entries of XML

Trying to read the following XML file in VB.net

<?xml version="1.0" encoding="utf-8"?>
<TrustedDomains>
  <Domain>one.co.uk</Domain>
  <Domain>two.co.uk</Domain>
</TrustedDomains>

My issue is it will only read one line and then moves on. My code is as follows.

        Dim XML As XDocument = XDocument.Load(XMLFile)
        For Each domainElement As XElement In XML.<TrustedDomains>
            Dim domain As String = domainElement.<Domain>.Value
            ListBox1.Items.Add(domain)
        Next

I have tried both XElement and XContainer . I can find many examples of reading an XML with multiple values, but nothing for a simple list.

If you are going to be using XML a lot I'd get acquainted with XElement and LINQ .

    Dim xe As XElement
    xe = XElement.Load(XMLFile)

    'this
    For Each domainElement As XElement In xe...<Domain>
        ListBox1.Items.Add(domainElement.Value)
    Next

    'or this
    ListBox1.Items.AddRange((From el In xe...<Domain>
                                Select el.Value).ToArray)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM