1

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
  • 1
    What version of VB/.NET? Commented Dec 28, 2009 at 16:12

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

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.
So, what about the "dict" have children or parents with the same tag (dict). But anyway, thanks for your reply! +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.