Results 1 to 7 of 7

Thread: XML woes

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2003
    Location
    Amsterdam, The Netherlands
    Posts
    53

    Question XML woes

    I am trying to do something that should be simple I guess, but I just cannto get it to work and have no clue how to get it to work.. Most likely I am overlooking the most simple solution, but I just cannot find it.

    Here's what I have:

    I have a XML file in this format:

    Code:
    <?xml version="1.0" encoding="iso-8859-1" ?>
    <FXGroup>
       <monfolder src="E:\_NEWS_\_incoming\muzak\Top40"/>
       <media src="E:\MP3\Top40\What's up - Who else.mp3" title ="What's up" artist ="Who else" _
    bpm ="110.34" gain ="00.00" duration ="03:46" comment ="Ehhh.." Format ="1" bitrate ="192" _
    year ="2003" id ="0001-02" pos ="1"/>
    </FXGroup>
    As you might understand there's more <media> tags.. What I want to do is to read all the media tags and put them in a listview.. I guess I would have to take the information in the tag apart myself but that's not the problem, the problem is getting the tag into an array.. without just reading the XML file line by line.. There must be a way to 'just' get each <media> tag until there are no more.. Confused?? so am I right now.. :^)

    I can get this XML file into a datagrid easy enough, but I want to use a listview..

    As I said I tried a number of approaches, but seem to be unable to get it to work.. But then again,like I said I am probably overlooking the simplest solution..

  2. #2
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    Can't really tell if you tried this or not, but I'd do something like this:
    VB Code:
    1. Dim xml As New XmlDocument()
    2. xml.Load("YourFile.xml")
    3. Dim xmlMediaNodeList As XmlNodeList = xml.DocumentElement.SelectNodes("//media")
    4. If Not xmlMediaNodeList Is Nothing Then
    5.   For Each xmlMediaNode As XmlNode In xmlMediaNodeList
    6.     'do something with the node...
    7.   Next
    8. End If

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2003
    Location
    Amsterdam, The Netherlands
    Posts
    53
    Although I see whee you're going I a still stuck and have no idea how to get the information in the <media> node.. does anyone have any pointers to where I can read up on this?

  4. #4
    Fanatic Member Redth's Avatar
    Join Date
    May 2001
    Location
    Ontario, Canada
    Posts
    551
    you might want to look into the XmlTextReader also... this is assuming you only need to read the info, and not write to the file at this stage...

    in that case, you'd loop through all the nodes... check the node.Name i think, and that would return Media for the <Media> tag....

    then you would want to use GetAttribute("src") and that would give you E:\_NEWS_\_incoming\muzak\Top40... and do the rest for the other attributes...

    i'll post a little example in a second here

  5. #5
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366

  6. #6
    Fanatic Member Redth's Avatar
    Join Date
    May 2001
    Location
    Ontario, Canada
    Posts
    551
    Ok, here should be an example for you to go off of....

    sorry it's in c#, i didn't even realize where i was posting at the time of writing it... should be easy enough to understand though...

    also don't quote me on the use of the ListViewItem.. i'm not even sure that's right.. i just wrote it off the top of my head... but here it goes:

    VB Code:
    1. ListViewItem newItem;
    2.  
    3. XmlTextReader xr = new XmlTextReader("C:\MyXmlFile.xml");
    4.  
    5. while (xr.Read())
    6. {
    7.     if (xr.Name.Equals("Media")
    8.     {
    9.         newItem = new ListViewItem("");
    10.  
    11.         newItem.SubItems.Add(xr.GetAttribute("src"));
    12.         newItem.SubItems.Add(xr.GetAttribute("title"));
    13.         newItem.SubItems.Add(xr.GetAttribute("bpm"0));
    14.         //etc. etc.. for all the other tags
    15.  
    16.         listView1.Items.Add(newItem);
    17.         newItem = null;
    18.     }
    19. }
    20.  
    21. xr.Close();
    22. xr = null;

  7. #7

    Thread Starter
    Member
    Join Date
    Nov 2003
    Location
    Amsterdam, The Netherlands
    Posts
    53
    That worked perfectly for me!.. Luckily I am not so much a newbie anymore I cannot read another languege..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width