tags:

views:

85

answers:

2

Hi, 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>
+1  A: 

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.

SLaks
But enough to take with tracklist and plist before you look for dict. Because, what if it is elimenter that tag dict without the TrackList, or if there is dict elimenter inside dict again.
sv88erik
What are you talking about?
SLaks
So, what about the "dict" have children or parents with the same tag (dict).But anyway, thanks for your reply! +1
sv88erik
+1  A: 

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

Wade73