|
-
Jan 13th, 2006, 08:44 AM
#1
Thread Starter
Fanatic Member
Create/Write XML File (Blank xml file created, nothing written)
I want to create a XML file. I have this code, but nothing is written into the file ... it only creates a blank xml file.
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim openFolder As New FolderBrowserDialog
Dim saveFile As New SaveFileDialog
Dim sw As IO.StreamWriter
Dim path As String
Dim file As String
Dim data As String
Dim images() As String
Try
Button1.Enabled = False
openFolder.ShowDialog()
saveFile.FileName = "*.xml"
saveFile.Filter = "XML File|*.xml"
saveFile.ShowDialog()
If openFolder.SelectedPath <> "" Then
path = openFolder.SelectedPath
If saveFile.FileName <> "" Then
file = saveFile.FileName
images = IO.Directory.GetFiles(path)
data = "<images directory=""photos/Nuit de la Joke"">" + vbNewLine
For i As Integer = 0 To images.Length - 1
If IO.Path.GetExtension(images(i)) = ".jpg" Then
data += "<imageNode jpegURL=""" & IO.Path.GetFileName(images(i)) & """ thumbURL=""" & IO.Path.GetFileName(images(i)) & """ title=""Title " & i & """</imageNode>" + vbNewLine
End If
Next
data += "</images>"
sw = New IO.StreamWriter(file)
sw.Write(data)
MsgBox(data)
End If
End If
Catch ex As Exception
MsgBox(ex.ToString)
Finally
Button1.Enabled = True
End Try
End Sub
-
Jan 13th, 2006, 08:54 AM
#2
Re: Create/Write XML File (Blank xml file created, nothing written)
you're going about creating an xml file all the wrong way... let me find my code that i use to create a new xml file and then you can use that to get you started. keep in mind there is a reason there's an XML namespace
-
Jan 13th, 2006, 08:58 AM
#3
Re: Create/Write XML File (Blank xml file created, nothing written)
ok like i said this is some code i use to do a particular process but you should be able to get the idea of how to correctly create an xml file from scatch.
let me know if you have any questions.
VB Code:
Private Sub XMLInformation(ByVal mt As Boolean, ByVal isbn As StringBuilder, ByVal rating As String, ByVal age As String, ByVal anno As String, ByVal reviewer As String, ByVal page As String, ByVal title As String)
'Determine if the xml file exists
If File.Exists("guide.xml") Then
Dim xdoc As New Xml.XmlDocument
xdoc.Load("guide.xml")
Dim xnlist As Xml.XmlNodeList = xdoc.SelectNodes("//review[@mt='1']")
If xnlist.Count > 0 Then
If mt Then
Dim bFound As Boolean = False
For Each xnode As Xml.XmlNode In xnlist
Application.DoEvents()
Dim xelement As Xml.XmlElement = DirectCast(xnode, Xml.XmlElement)
If xelement.GetElementsByTagName("anno")(0).InnerText.ToLower.Equals(anno.ToLower.Trim) Then
bFound = True
For Each s As String In isbn.ToString.Split(";"c)
Application.DoEvents()
If s.Length <> 0 Then
Dim xIsbn As Xml.XmlElement = xdoc.CreateElement("isbn")
xelement.SelectSingleNode("./isbns").AppendChild(xIsbn)
xIsbn.SetAttribute("number", s.Trim)
End If
Next
'Setup a new titles node
Dim xTitle As Xml.XmlElement = xdoc.CreateElement("title")
xelement.SelectSingleNode("./titles").AppendChild(xTitle)
xTitle.SetAttribute("value", title.Trim)
xTitle.SetAttribute("page_count", page.Trim)
Exit For
End If
Next
If Not bFound Then
CreateNewReviewNode(xdoc, mt, isbn, rating, age, anno, reviewer, page, title)
End If
Else
CreateNewReviewNode(xdoc, mt, isbn, rating, age, anno, reviewer, page, title)
End If
Else
CreateNewReviewNode(xdoc, mt, isbn, rating, age, anno, reviewer, page, title)
End If
xdoc.Save("guide.xml")
Else
Dim xwtr As New Xml.XmlTextWriter("guide.xml", Encoding.UTF8)
xwtr.Formatting = Xml.Formatting.Indented
xwtr.Indentation = 4
xwtr.QuoteChar = """"c
xwtr.WriteStartDocument()
xwtr.WriteStartElement("guide")
xwtr.WriteEndElement()
xwtr.WriteEndDocument()
xwtr.Close()
XMLInformation(mt, isbn, rating, age, anno, reviewer, page, title)
End If
End Sub
Private Sub CreateNewReviewNode(ByVal xdoc As Xml.XmlDocument, ByVal mt As Boolean, ByVal isbn As StringBuilder, ByVal rating As String, ByVal age As String, ByVal anno As String, ByVal reviewer As String, ByVal page As String, ByVal title As String)
'Setup a new review element node
Dim xReview As Xml.XmlElement = xdoc.CreateElement("review")
xdoc.DocumentElement.AppendChild(xReview)
'Add the attributes
xReview.SetAttribute("mt", Convert.ToByte(mt).ToString.Trim)
xReview.SetAttribute("rating", rating.Trim)
xReview.SetAttribute("age", age)
xReview.SetAttribute("reviewer", reviewer.Trim)
'Setup a new isbns node
Dim xIsbns As Xml.XmlElement = xdoc.CreateElement("isbns")
For Each s As String In isbn.ToString.Split(";"c)
Application.DoEvents()
If s.Length <> 0 Then
Dim xIsbn As Xml.XmlElement = xdoc.CreateElement("isbn")
xIsbns.AppendChild(xIsbn)
xIsbn.SetAttribute("number", s.Trim)
End If
Next
xReview.AppendChild(xIsbns)
'Setup a new titles node
Dim xTitles As Xml.XmlElement = xdoc.CreateElement("titles")
Dim xTitle As Xml.XmlElement = xdoc.CreateElement("title")
xTitles.AppendChild(xTitle)
xTitle.SetAttribute("value", title.Trim)
xTitle.SetAttribute("page_count", page.Trim)
xReview.AppendChild(xTitles)
'Setup a new anno node
Dim xAnno As Xml.XmlElement = xdoc.CreateElement("anno")
'Setup a xml text object
Dim xAnnoText As Xml.XmlText = xdoc.CreateTextNode(anno.Trim)
xAnno.AppendChild(xAnnoText)
xReview.AppendChild(xAnno)
End Sub
-
Jan 13th, 2006, 09:08 AM
#4
Thread Starter
Fanatic Member
Re: Create/Write XML File (Blank xml file created, nothing written)
thank you
It's the first time I work with XML File. I don't know what it is, I don't why we use this, I don't understand anything. Thing is that I have a Flash ImageGallery that use XML file to list picture, with over 200 picture, I didnt want to write it all, so I created this litttle app.
thanks for you code.
-
Jan 13th, 2006, 09:12 AM
#5
Re: Create/Write XML File (Blank xml file created, nothing written)
np like i said if you don't understand anything or have a question just let me know... fyi... when i first started creating xml file i did it the same way, by basically creating a new xml file and writing all the xml code, attr, and values by hand rather then letting the .net framework work for me... so to put it.
-
Jan 13th, 2006, 09:18 AM
#6
Thread Starter
Fanatic Member
Re: Create/Write XML File (Blank xml file created, nothing written)
well, I'm a bit lost.
Can you tell me what these two sub are doing and where should I put it??
-
Jan 13th, 2006, 09:27 AM
#7
Re: Create/Write XML File (Blank xml file created, nothing written)
don't worry about the methods and what they really do... look more at the different xml classes that i use in order to create an xml file that was the purpose of posting my code that i use...
1) in the XMLInformation method i use it recursive method... go to the else statement, that's where the guide.xml file is first created. it then calls itself again.
2) now that we have the file created we need to start adding the elements to the xml file.
-
Jan 13th, 2006, 09:33 AM
#8
Thread Starter
Fanatic Member
Re: Create/Write XML File (Blank xml file created, nothing written)
thanks for your help boy.
it's not the first time you help me, since i must spread around before giving you some rating!
-
Jan 13th, 2006, 09:35 AM
#9
Re: Create/Write XML File (Blank xml file created, nothing written)
did you get it because if not i'm working on a solution for you
-
Jan 13th, 2006, 09:57 AM
#10
Re: Create/Write XML File (Blank xml file created, nothing written)
here's a solution for you
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim openFolder As New FolderBrowserDialog
Dim saveFile As New SaveFileDialog
Dim sw As IO.StreamWriter
Dim path As String
Dim file As String
Dim data As String
Dim images() As String
Try
Button1.Enabled = False
openFolder.ShowDialog()
saveFile.FileName = "*.xml"
saveFile.Filter = "XML File|*.xml"
saveFile.ShowDialog()
If openFolder.SelectedPath <> "" Then
path = openFolder.SelectedPath
If saveFile.FileName <> "" Then
file = saveFile.FileName
images = IO.Directory.GetFiles(path)
'Create the xml file
If Not IO.File.Exists(file) Then
Dim xwtr As New Xml.XmlTextWriter(file, System.Text.Encoding.UTF8)
xwtr.Formatting = Xml.Formatting.Indented
xwtr.Indentation = 4
xwtr.QuoteChar = """"c
xwtr.WriteStartDocument()
'use whatever you want for the root node
xwtr.WriteStartElement("ImageGallery")
xwtr.WriteEndElement()
xwtr.WriteEndDocument()
xwtr.Close()
End If
' data = "<images directory=""photos/Nuit de la Joke"">" + vbNewLine
Dim xdoc As New Xml.XmlDocument
xdoc.Load(file)
For i As Integer = 0 To images.Length - 1
If IO.Path.GetExtension(images(i)) = ".jpg" Then
'Setup a new image element node
Dim xImage As Xml.XmlElement = xdoc.CreateElement("imageNode")
xdoc.DocumentElement.AppendChild(xImage)
'Add the attributes to the image node
xImage.SetAttribute("jpegURL", IO.Path.GetFileName(images(i)))
xImage.SetAttribute("thumbURL", IO.Path.GetFileName(images(i)))
xImage.SetAttribute("title", "Title " + i.ToString)
'Save the new value to the xml file
xdoc.Save(file)
End If
Next
' data += "</images>"
' sw = New IO.StreamWriter(file)
' sw.Write(data)
' MsgBox(data)
End If
End If
Catch ex As Exception
MsgBox(ex.ToString)
Finally
Button1.Enabled = True
End Try
End Sub
here is the xml if produced.
Code:
<?xml version="1.0" encoding="utf-8"?>
<ImageGallery>
<imageNode jpegURL="100_1103.jpg" thumbURL="100_1103.jpg" title="Title 0" />
<imageNode jpegURL="100_1113.jpg" thumbURL="100_1113.jpg" title="Title 1" />
<imageNode jpegURL="100_1114.jpg" thumbURL="100_1114.jpg" title="Title 2" />
<imageNode jpegURL="100_1115.jpg" thumbURL="100_1115.jpg" title="Title 3" />
<imageNode jpegURL="100_1116.jpg" thumbURL="100_1116.jpg" title="Title 4" />
</ImageGallery>
-
Jan 13th, 2006, 10:03 AM
#11
Thread Starter
Fanatic Member
Re: Create/Write XML File (Blank xml file created, nothing written)
hay! thank again man! really, thanks for your time! it works flawless
-
Jan 13th, 2006, 10:04 AM
#12
Re: Create/Write XML File (Blank xml file created, nothing written)
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
|