eriksv88
eriksv88

Reputation: 3526

VB.NET - Parsing XML

how can I get a ArrayList or List of key and value of the Name, ID and description from the xml file below?

I do not really understand how the handling of elimenter done in VB.NET based on logic in javascript.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>


<Document>
 <TrackList>
  <plist>
   <array>

    <dict>
     <key>Name</key><string>Michael Jackson</string>
     <key>ID</key><integer>22</integer>
     <key>description</key><string>Some text</string>
    </dict>

    <dict>
     <key>Name</key><string>Pink Floyd</string>
     <key>ID</key><integer>52</integer>
     <key>description</key><string>Some text</string>
    </dict>
   <array>

  </plist>
 </TrackList>
</Document>

Upvotes: 1

Views: 1403

Answers (2)

Wade73
Wade73

Reputation: 4519

If you are using VB then you can use some built-in constructs to make the query easier. I built a small app that returns a anonymous class. Now the assumption is that the structure remains the same, so dict will always have six elements.

    Dim XML = <?xml version="1.0" encoding="UTF-8" standalone="no"?>
              <Document>
                  <TrackList>
                      <plist>
                          <array>
                              <dict>
                                  <key>Name</key>
                                  <string>Michael Jackson</string>
                                  <key>ID</key>
                                  <integer>22</integer>
                                  <key>description</key>
                                  <string>Some text</string>
                              </dict>
                              <dict>
                                  <key>Name</key><string>Pink Floyd</string>
                                  <key>ID</key><integer>52</integer>
                                  <key>description</key><string>Some text</string>
                              </dict>
                          </array>
                      </plist>
                  </TrackList>
              </Document>

    Dim Elements = From xmlelement In XML...<dict> _
                   Select Name = xmlelement.Elements.ElementAt(1).Value, _
                    ID = xmlelement.Elements.ElementAt(3).Value, _
                    Description = xmlelement.Elements.ElementAt(5).Value

An alternative would be to get the child elements for the dict element. Then you can cursor through them to build the key/value pairs.

    Dim Elements = From xmlelement In XML...<dict> _
                   Select xmlelement.Elements.tolist()

HTH

Upvotes: 1

SLaks
SLaks

Reputation: 888077

You can use XLINQ, like this:

Dim xdoc = XDocument.Load(...)
Dim dicts = xdoc...<dict>.Select(Function(dict) dict.Elements())

This will give you a nested IEnumerable of XElements.

Upvotes: 1

Related Questions