May 7th, 2012, 04:45 PM
#1
Thread Starter
Member
[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
May 7th, 2012, 05:07 PM
#2
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?
Coding Examples:
Features:
Online Games:
Compiled Games:
May 7th, 2012, 05:10 PM
#3
Thread Starter
Member
May 7th, 2012, 05:22 PM
#4
Re: Read write xml
try this:
vb Code:
Imports System.Xml
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim outPut As String = ""
Dim baseFolder As String = "C:\Users\Paul\Desktop\movies"
'read each xml file
For Each subFolder As String In IO.Directory.GetDirectories(baseFolder)
Dim xml As New XmlDocument
xml.Load(IO.Path.Combine(subFolder, "movie.xml"))
'extract values
outPut &= xml.SelectSingleNode("Title/LocalTitle").InnerText & ","
outPut &= xml.SelectSingleNode("Title/AspectRatio").InnerText & Environment.NewLine
Next
'save outPut as excel CSV
IO.File.WriteAllText("outPut.csv", outPut)
End Sub
End Class
Coding Examples:
Features:
Online Games:
Compiled Games:
May 7th, 2012, 05:30 PM
#5
Thread Starter
Member
Re: Read write xml
Thanks for your quick response. Will try and let you know.
May 7th, 2012, 05:57 PM
#6
Thread Starter
Member
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?
May 7th, 2012, 06:11 PM
#7
Re: Read write xml
what are the names of the xml files? you told me they were all called movie.xml
Coding Examples:
Features:
Online Games:
Compiled Games:
May 7th, 2012, 06:12 PM
#8
Re: Read write xml
you can put the string variable outPut in a textbox:
Coding Examples:
Features:
Online Games:
Compiled Games:
May 7th, 2012, 06:34 PM
#9
Thread Starter
Member
Re: Read write xml
Originally Posted by
.paul.
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
May 7th, 2012, 06:44 PM
#10
Re: Read write xml
there must be a sub folder in your base folder, that doesn't contain an xml file
Coding Examples:
Features:
Online Games:
Compiled Games:
May 7th, 2012, 06:50 PM
#11
Thread Starter
Member
Re: Read write xml
Originally Posted by
.paul.
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.
May 7th, 2012, 07:02 PM
#12
Re: Read write xml
ok. try this:
vb Code:
Imports System.Xml
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim outPut As String = ""
Dim baseFolder As String = "C:\Users\Paul\Desktop\movies"
'read each xml file
For Each subFolder As String In IO.Directory.GetDirectories(baseFolder)
dim xmlFileName as string = IO.Path.Combine(subFolder, "movie.xml")
if io.file.exists(xmlFileName) then
Dim xml As New XmlDocument
xml.Load()
'extract values
outPut &= xml.SelectSingleNode("Title/LocalTitle").InnerText & ","
outPut &= xml.SelectSingleNode("Title/AspectRatio").InnerText & Environment.NewLine
end if
Next
'save outPut as excel CSV
'IO.File.WriteAllText("outPut.csv", outPut)
textbox1.text = outPut
End Sub
End Class
Coding Examples:
Features:
Online Games:
Compiled Games:
May 7th, 2012, 07:04 PM
#13
Re: Read write xml
yes, you could use 2 buttons + a textbox.
are you sure the xml files are literally called "movie.xml"?
Coding Examples:
Features:
Online Games:
Compiled Games:
May 7th, 2012, 07:09 PM
#14
Thread Starter
Member
Re: Read write xml
Originally Posted by
lag22
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?
May 7th, 2012, 07:25 PM
#15
Thread Starter
Member
Re: Read write xml
Originally Posted by
.paul.
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
May 7th, 2012, 07:26 PM
#16
Re: Read write xml
you could use a listview (readonly). add a listview + 2 buttons to your form:
vb Code:
Imports System.Xml
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim baseFolder As String = "C:\Users\Paul\Desktop\movies"
'read each xml file
For Each subFolder As String In IO.Directory.GetDirectories(baseFolder)
Dim xml As New XmlDocument
xml.Load(IO.Path.Combine(subFolder, "movie.xml"))
'extract values + add to listview
Dim lvi As New ListViewItem(xml.SelectSingleNode("Title/LocalTitle").InnerText)
lvi.SubItems.Add(xml.SelectSingleNode("Title/AspectRatio").InnerText)
ListView1.Items.Add(lvi)
Next
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListView1.View = View.Details
ListView1.Columns.Add("Title")
ListView1.Columns.Add("AspectRatio")
ListView1.Columns(0).Width = 150
ListView1.Columns(1).Width = 150
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'save to csv
Dim outPut As String = ""
For Each lvi As ListViewItem In ListView1.Items
outPut &= String.Join(",", lvi.SubItems.Cast(Of ListViewItem.ListViewSubItem).Select(Function(si) si.Text).ToArray) & Environment.NewLine
Next
'save outPut as excel CSV
IO.File.WriteAllText("outPut.csv", outPut)
Process.Start("outPut.csv")
End Sub
End Class
Coding Examples:
Features:
Online Games:
Compiled Games:
May 7th, 2012, 07:59 PM
#17
Thread Starter
Member
Re: Read write xml
Originally Posted by
.paul.
you could use a listview (readonly). add a listview + 2 buttons to your form:
vb Code:
Imports System.Xml
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim baseFolder As String = "C:\Users\Paul\Desktop\movies"
'read each xml file
For Each subFolder As String In IO.Directory.GetDirectories(baseFolder)
Dim xml As New XmlDocument
xml.Load(IO.Path.Combine(subFolder, "movie.xml"))
When I click Button1, nothing appears in List
'extract values + add to listview
Dim lvi As New ListViewItem(xml.SelectSingleNode("Title/LocalTitle").InnerText)
lvi.SubItems.Add(xml.SelectSingleNode("Title/AspectRatio").InnerText)
ListView1.Items.Add(lvi)
Next
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListView1.View = View.Details
ListView1.Columns.Add("Title")
ListView1.Columns.Add("AspectRatio")
ListView1.Columns(0).Width = 150
ListView1.Columns(1).Width = 150
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'save to csv
Dim outPut As String = ""
For Each lvi As ListViewItem In ListView1.Items
outPut &= String.Join(",", lvi.SubItems.Cast(Of ListViewItem.ListViewSubItem).Select(Function(si) si.Text).ToArray) & Environment.NewLine
Next
'save outPut as excel CSV
IO.File.WriteAllText("outPut.csv", outPut)
Process.Start("outPut.csv")
End Sub
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?
May 7th, 2012, 08:04 PM
#18
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
Coding Examples:
Features:
Online Games:
Compiled Games:
May 7th, 2012, 08:30 PM
#19
Thread Starter
Member
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
May 7th, 2012, 08:33 PM
#20
Re: Read write xml
it worked when i wrote it + tested it.
can you post your code that isn't working?
Coding Examples:
Features:
Online Games:
Compiled Games:
May 7th, 2012, 08:41 PM
#21
Thread Starter
Member
Re: Read write xml
Here is the code;
code.txt
May 7th, 2012, 08:43 PM
#22
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
Coding Examples:
Features:
Online Games:
Compiled Games:
May 7th, 2012, 08:52 PM
#23
Thread Starter
Member
Re: Read write xml
Originally Posted by
.paul.
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
May 7th, 2012, 08:57 PM
#24
Re: Read write xml
it should be working now. your picture didn't upload + xmlMovie.txt is your original code from your original post
Coding Examples:
Features:
Online Games:
Compiled Games:
May 7th, 2012, 08:58 PM
#25
Thread Starter
Member
Re: Read write xml
Sorry.Something went wrong with the attachment;
http://dl.dropbox.com/u/19298307/xml.JPG
May 7th, 2012, 08:59 PM
#26
Coding Examples:
Features:
Online Games:
Compiled Games:
May 7th, 2012, 09:27 PM
#27
Thread Starter
Member
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
May 7th, 2012, 09:45 PM
#28
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
Coding Examples:
Features:
Online Games:
Compiled Games:
May 7th, 2012, 09:49 PM
#29
Thread Starter
Member
May 9th, 2012, 03:09 PM
#30
Thread Starter
Member
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.
May 9th, 2012, 03:19 PM
#31
Re: Read write xml
you could use something like this:
vb Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'browse for folder
Dim fbd As New FolderBrowserDialog
If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
TextBox1.Text = fbd.SelectedPath
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If IO.Directory.Exists(TextBox1.Text) Then
'button1 has already been clicked
'your code here
Else
MessageBox.Show("Please select a folder first", My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly, False)
End If
End Sub
End Class
Coding Examples:
Features:
Online Games:
Compiled Games:
May 9th, 2012, 03:43 PM
#32
Thread Starter
Member
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.
May 9th, 2012, 04:00 PM
#33
Re: Read write xml
have you set the dialogresult property for the button?
you need to give more information. post your code...
Coding Examples:
Features:
Online Games:
Compiled Games:
May 9th, 2012, 04:13 PM
#34
Thread Starter
Member
Re: Read write xml
What should the dialogresult property be set as?
May 9th, 2012, 04:16 PM
#35
Coding Examples:
Features:
Online Games:
Compiled Games:
May 10th, 2012, 12:18 AM
#36
Thread Starter
Member
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.
May 18th, 2012, 08:11 PM
#37
Thread Starter
Member
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
Forum Rules
Click Here to Expand Forum to Full Width