[RESOLVED] Read write xml-VBForums
Results 1 to 37 of 37

Thread: [RESOLVED] Read write xml

  1. #1

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Resolved [RESOLVED] Read write xml

    I am new to vb.net and would like some help.

    I have a folder called movies. In this folder is a collection of different movies each in their own folder with their own xml file called movie.xml.

    I would like to extract lets say the localtitle and aspectratio from each movie xml and send to Excel. What code would I need to achieve this?

    So far I have the attached code which reads only one file and it is directly from the xml outside of a folder.

    Samples of code and xml files attached. Thanks for any help.

    xmlMovie.txt

    movie.xml sample.txt

  2. #2
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,353

    Re: Read write xml

    what is the structure of your movies folder?
    am i right in assuming that it has a subfolder for each movie + each subfolder has an xml file called movie.xml?

    you want to extract localtitle and aspectratio from each of those subfolders + output to excel?

  3. #3

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Re: Read write xml

    Yes you are correct.

  4. #4
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,353

    Re: Read write xml

    try this:

    vb Code:
    1. Imports System.Xml
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.         Dim outPut As String = ""
    7.         Dim baseFolder As String = "C:\Users\Paul\Desktop\movies"
    8.  
    9.         'read each xml file
    10.         For Each subFolder As String In IO.Directory.GetDirectories(baseFolder)
    11.             Dim xml As New XmlDocument
    12.             xml.Load(IO.Path.Combine(subFolder, "movie.xml"))
    13.  
    14.             'extract values
    15.             outPut &= xml.SelectSingleNode("Title/LocalTitle").InnerText & ","
    16.             outPut &= xml.SelectSingleNode("Title/AspectRatio").InnerText & Environment.NewLine
    17.         Next
    18.  
    19.         'save outPut as excel CSV
    20.         IO.File.WriteAllText("outPut.csv", outPut)
    21.     End Sub
    22.  
    23. End Class

  5. #5

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Re: Read write xml

    Thanks for your quick response. Will try and let you know.

  6. #6

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Re: Read write xml

    I am getting the following:
    File not foundexception was unhandled
    Question; Can the output be shown on the form and then by another button saved?

  7. #7
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,353

    Re: Read write xml

    what are the names of the xml files? you told me they were all called movie.xml

  8. #8
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,353

    Re: Read write xml

    you can put the string variable outPut in a textbox:

    vb Code:
    1. textbox1.text = outPut

  9. #9

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Re: Read write xml

    Quote Originally Posted by .paul. View Post
    what are the names of the xml files? you told me they were all called movie.xml
    Yes. They all have the same name: movie.xml

  10. #10
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,353

    Re: Read write xml

    there must be a sub folder in your base folder, that doesn't contain an xml file

  11. #11

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Re: Read write xml

    Quote Originally Posted by .paul. View Post
    there must be a sub folder in your base folder, that doesn't contain an xml file
    I am using a test folder with only three sub folders. I will check the format of the xml files. In the interim, can you indicate which line to place the TextBox output? Am I correct that the form will have a text box and two buttons, one to start the action and another to save the results?

    Thanks for your help.

  12. #12
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,353

    Re: Read write xml

    ok. try this:

    vb Code:
    1. Imports System.Xml
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.         Dim outPut As String = ""
    7.         Dim baseFolder As String = "C:\Users\Paul\Desktop\movies"
    8.  
    9.         'read each xml file
    10.         For Each subFolder As String In IO.Directory.GetDirectories(baseFolder)
    11.  
    12.             dim xmlFileName as string = IO.Path.Combine(subFolder, "movie.xml")
    13.  
    14.             if io.file.exists(xmlFileName) then    
    15.                 Dim xml As New XmlDocument
    16.                 xml.Load()
    17.  
    18.                 'extract values
    19.                 outPut &= xml.SelectSingleNode("Title/LocalTitle").InnerText & ","
    20.                 outPut &= xml.SelectSingleNode("Title/AspectRatio").InnerText & Environment.NewLine
    21.             end if
    22.         Next
    23.  
    24.         'save outPut as excel CSV
    25.         'IO.File.WriteAllText("outPut.csv", outPut)
    26.         textbox1.text = outPut
    27.     End Sub
    28.  
    29. End Class

  13. #13
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,353

    Re: Read write xml

    yes, you could use 2 buttons + a textbox.

    are you sure the xml files are literally called "movie.xml"?

  14. #14

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Re: Read write xml

    Quote Originally Posted by lag22 View Post
    I am using a test folder with only three sub folders. I will check the format of the xml files. In the interim, can you indicate which line to place the TextBox output? Am I correct that the form will have a text box and two buttons, one to start the action and another to save the results?

    Thanks for your help.
    I deleted the folder that was the exception and it worked.

    Ok. I think I am almost there. Thanks for your patience.

    The info appears in the textbox but jumbled. Is there a way to place LocalTitle in one column and AspectRation in another?

    Also how can I save? Do I need a save button?

  15. #15

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Re: Read write xml

    Quote Originally Posted by .paul. View Post
    yes, you could use 2 buttons + a textbox.

    are you sure the xml files are literally called "movie.xml"?
    Yes. The name of the xml file is: movie.xml

  16. #16
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,353

    Re: Read write xml

    you could use a listview (readonly). add a listview + 2 buttons to your form:

    vb Code:
    1. Imports System.Xml
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.         Dim baseFolder As String = "C:\Users\Paul\Desktop\movies"
    7.  
    8.         'read each xml file
    9.         For Each subFolder As String In IO.Directory.GetDirectories(baseFolder)
    10.             Dim xml As New XmlDocument
    11.             xml.Load(IO.Path.Combine(subFolder, "movie.xml"))
    12.  
    13.             'extract values + add to listview
    14.             Dim lvi As New ListViewItem(xml.SelectSingleNode("Title/LocalTitle").InnerText)
    15.             lvi.SubItems.Add(xml.SelectSingleNode("Title/AspectRatio").InnerText)
    16.             ListView1.Items.Add(lvi)
    17.  
    18.         Next
    19.  
    20.     End Sub
    21.  
    22.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    23.         ListView1.View = View.Details
    24.         ListView1.Columns.Add("Title")
    25.         ListView1.Columns.Add("AspectRatio")
    26.         ListView1.Columns(0).Width = 150
    27.         ListView1.Columns(1).Width = 150
    28.     End Sub
    29.  
    30.  
    31.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    32.         'save to csv
    33.         Dim outPut As String = ""
    34.  
    35.         For Each lvi As ListViewItem In ListView1.Items
    36.             outPut &= String.Join(",", lvi.SubItems.Cast(Of ListViewItem.ListViewSubItem).Select(Function(si) si.Text).ToArray) & Environment.NewLine
    37.         Next
    38.  
    39.         'save outPut as excel CSV
    40.         IO.File.WriteAllText("outPut.csv", outPut)
    41.         Process.Start("outPut.csv")
    42.     End Sub
    43. End Class

  17. #17

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Re: Read write xml

    Quote Originally Posted by .paul. View Post
    you could use a listview (readonly). add a listview + 2 buttons to your form:

    vb Code:
    1. Imports System.Xml
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.         Dim baseFolder As String = "C:\Users\Paul\Desktop\movies"
    7.  
    8.         'read each xml file
    9.         For Each subFolder As String In IO.Directory.GetDirectories(baseFolder)
    10.             Dim xml As New XmlDocument
    11.             xml.Load(IO.Path.Combine(subFolder, "movie.xml"))
    12.  
    13.  
    14.  
    15. When I click Button1, nothing appears in List
    16.  
    17.             'extract values + add to listview
    18.             Dim lvi As New ListViewItem(xml.SelectSingleNode("Title/LocalTitle").InnerText)
    19.             lvi.SubItems.Add(xml.SelectSingleNode("Title/AspectRatio").InnerText)
    20.             ListView1.Items.Add(lvi)
    21.  
    22.         Next
    23.  
    24.     End Sub
    25.  
    26.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    27.         ListView1.View = View.Details
    28.         ListView1.Columns.Add("Title")
    29.         ListView1.Columns.Add("AspectRatio")
    30.         ListView1.Columns(0).Width = 150
    31.         ListView1.Columns(1).Width = 150
    32.     End Sub
    33.  
    34.  
    35.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    36.         'save to csv
    37.         Dim outPut As String = ""
    38.  
    39.         For Each lvi As ListViewItem In ListView1.Items
    40.             outPut &= String.Join(",", lvi.SubItems.Cast(Of ListViewItem.ListViewSubItem).Select(Function(si) si.Text).ToArray) & Environment.NewLine
    41.         Next
    42.  
    43.         'save outPut as excel CSV
    44.         IO.File.WriteAllText("outPut.csv", outPut)
    45.         Process.Start("outPut.csv")
    46.     End Sub
    47. End Class


    1.When I click Button1, nothing appears in ListView1.

    2.Should a save as box appear after clicking Button2 to indicate where to save?

  18. #18
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,353

    Re: Read write xml

    1\

    in your design time view, doubleclick button1.
    if it doesn't take you to the handler you pasted, delete the new handler it's created + doubleclick button1 again until it takes you to the handler you pasted

    2\ no it's hardcoded. here's how to add + use a savefiledialog:

    http://www.scproject.biz/Using%20Dialogs.htm#bm3

  19. #19

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Re: Read write xml

    This is what appears in the listView. There are 2 sub folders. The localtitles appear but not the aspectratios. Saving shows all. Is this correct?


    http://dl.dropbox.com/u/19298307/xml.JPG

  20. #20
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,353

    Re: Read write xml

    it worked when i wrote it + tested it.
    can you post your code that isn't working?

  21. #21

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Re: Read write xml

    Here is the code;

    code.txt

  22. #22
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,353

    Re: Read write xml

    you're missing a handles clause:

    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) handles mybase.load

  23. #23

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Re: Read write xml

    Quote Originally Posted by .paul. View Post
    you're missing a handles clause:

    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) handles mybase.load
    Thanks very much for all your help. I am beginning to understand a little. Here is the latest:

    Attached Files Attached Files

  24. #24
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,353

    Re: Read write xml

    it should be working now. your picture didn't upload + xmlMovie.txt is your original code from your original post

  25. #25

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Re: Read write xml

    Sorry.Something went wrong with the attachment;

    http://dl.dropbox.com/u/19298307/xml.JPG

  26. #26
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,353

    Re: Read write xml

    so it's working then?

  27. #27

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Re: Read write xml

    It's working. However, this is what the the file looks like when saved. "The" from the name of the movie appears in the column with the aspectratio and then pushes thos ratios over to the next column.

    http://dl.dropbox.com/u/19298307/File.JPG

  28. #28
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,353

    Re: Read write xml

    let me guess. the line in question is:

    Adjustment Bureau, The

    right?

    csv is comma separated values + any commas are treated as new columns.
    you need to remove the comma or replace the comma with a different character

  29. #29

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Re: Read write xml

    Thanks. That worked.

  30. #30

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Re: Read write xml

    I really appreciated your help and hope I am not overdoing it by seeking it again.

    I have tweaked the code and almost have it where I want it. Presently there are 2 buttons. Button1 and button2. Button1 will browse for a folder and place that folder in a textbox. Button2 will select data from folder in textbox.

    Button1 has to be clicked first. If button2 is clicked before button1 then there is an error.

    What I wish to achieve if button2 is clicked first:
    MessageBox to indicate that Button1 should be clicked first. However on closing MessageBox, I would like the form to remain open so I can then click button1.

    Thanks for any help.

  31. #31
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,353

    Re: Read write xml

    you could use something like this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         'browse for folder
    5.         Dim fbd As New FolderBrowserDialog
    6.         If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
    7.             TextBox1.Text = fbd.SelectedPath
    8.         End If
    9.     End Sub
    10.  
    11.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    12.         If IO.Directory.Exists(TextBox1.Text) Then
    13.             'button1 has already been clicked
    14.             'your code here
    15.         Else
    16.             MessageBox.Show("Please select a folder first", My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly, False)
    17.         End If
    18.     End Sub
    19.  
    20. End Class

  32. #32

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Re: Read write xml

    Thanks. However when I click in the messagebox, it closes the form. Is there a way to close the messagebox but keep the form open? That way it would not be necessay to reopen the form to select folder.

  33. #33
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,353

    Re: Read write xml

    have you set the dialogresult property for the button?

    you need to give more information. post your code...

  34. #34

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Re: Read write xml

    What should the dialogresult property be set as?

  35. #35
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,353

    Re: Read write xml

    it shouldn't be set.

  36. #36

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Re: Read write xml

    Thanks. I got things to work. Late now. Will test further tomorrow. Adjusted the code and should be ok.
    By the way, do you have any suggestions for good reading so I can improve my knowledge.

  37. #37

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Re: Read write xml

    All works well. Thanks.

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.