Results 1 to 12 of 12

Thread: Create/Write XML File (Blank xml file created, nothing written)

  1. #1

    Thread Starter
    Fanatic Member jcavard's Avatar
    Join Date
    Jul 2005
    Location
    Quebec, CANADA
    Posts
    534

    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim openFolder As New FolderBrowserDialog
    3.         Dim saveFile As New SaveFileDialog
    4.  
    5.         Dim sw As IO.StreamWriter
    6.  
    7.         Dim path As String
    8.         Dim file As String
    9.         Dim data As String
    10.         Dim images() As String
    11.  
    12.         Try
    13.             Button1.Enabled = False
    14.             openFolder.ShowDialog()
    15.  
    16.             saveFile.FileName = "*.xml"
    17.             saveFile.Filter = "XML File|*.xml"
    18.             saveFile.ShowDialog()
    19.  
    20.             If openFolder.SelectedPath <> "" Then
    21.                 path = openFolder.SelectedPath
    22.  
    23.                 If saveFile.FileName <> "" Then
    24.                     file = saveFile.FileName
    25.  
    26.                     images = IO.Directory.GetFiles(path)
    27.  
    28.  
    29.                     data = "<images directory=""photos/Nuit de la Joke"">" + vbNewLine
    30.                     For i As Integer = 0 To images.Length - 1
    31.                         If IO.Path.GetExtension(images(i)) = ".jpg" Then
    32.                             data += "<imageNode jpegURL=""" & IO.Path.GetFileName(images(i)) & """ thumbURL=""" & IO.Path.GetFileName(images(i)) & """ title=""Title " & i & """</imageNode>" + vbNewLine
    33.                         End If
    34.                     Next
    35.                     data += "</images>"
    36.  
    37.                     sw = New IO.StreamWriter(file)
    38.                     sw.Write(data)
    39.  
    40.                     MsgBox(data)
    41.                 End If
    42.             End If
    43.         Catch ex As Exception
    44.             MsgBox(ex.ToString)
    45.         Finally
    46.             Button1.Enabled = True
    47.         End Try
    48.  
    49.  
    50.     End Sub
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

    accoustic emo-rock band: a tailormade fable

    Visual Studio 2003 / Framework 1.1

  2. #2
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    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

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  3. #3
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    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:
    1. 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)
    2.                 'Determine if the xml file exists
    3.                 If File.Exists("guide.xml") Then
    4.                     Dim xdoc As New Xml.XmlDocument
    5.  
    6.                     xdoc.Load("guide.xml")
    7.  
    8.                     Dim xnlist As Xml.XmlNodeList = xdoc.SelectNodes("//review[@mt='1']")
    9.  
    10.                     If xnlist.Count > 0 Then
    11.                         If mt Then
    12.                             Dim bFound As Boolean = False
    13.  
    14.                             For Each xnode As Xml.XmlNode In xnlist
    15.                                 Application.DoEvents()
    16.                                 Dim xelement As Xml.XmlElement = DirectCast(xnode, Xml.XmlElement)
    17.  
    18.                                 If xelement.GetElementsByTagName("anno")(0).InnerText.ToLower.Equals(anno.ToLower.Trim) Then
    19.                                     bFound = True
    20.  
    21.  
    22.                                     For Each s As String In isbn.ToString.Split(";"c)
    23.                                         Application.DoEvents()
    24.                                         If s.Length <> 0 Then
    25.                                             Dim xIsbn As Xml.XmlElement = xdoc.CreateElement("isbn")
    26.                                             xelement.SelectSingleNode("./isbns").AppendChild(xIsbn)
    27.                                             xIsbn.SetAttribute("number", s.Trim)
    28.                                         End If
    29.                                     Next
    30.  
    31.                                     'Setup a new titles node
    32.                                     Dim xTitle As Xml.XmlElement = xdoc.CreateElement("title")
    33.                                     xelement.SelectSingleNode("./titles").AppendChild(xTitle)
    34.                                     xTitle.SetAttribute("value", title.Trim)
    35.                                     xTitle.SetAttribute("page_count", page.Trim)
    36.  
    37.                                     Exit For
    38.                                 End If
    39.                             Next
    40.                             If Not bFound Then
    41.                                 CreateNewReviewNode(xdoc, mt, isbn, rating, age, anno, reviewer, page, title)
    42.                             End If
    43.                         Else
    44.                             CreateNewReviewNode(xdoc, mt, isbn, rating, age, anno, reviewer, page, title)
    45.                         End If
    46.                     Else
    47.                         CreateNewReviewNode(xdoc, mt, isbn, rating, age, anno, reviewer, page, title)
    48.                     End If
    49.  
    50.                     xdoc.Save("guide.xml")
    51.                 Else
    52.                     Dim xwtr As New Xml.XmlTextWriter("guide.xml", Encoding.UTF8)
    53.  
    54.                     xwtr.Formatting = Xml.Formatting.Indented
    55.                     xwtr.Indentation = 4
    56.                     xwtr.QuoteChar = """"c
    57.                     xwtr.WriteStartDocument()
    58.                     xwtr.WriteStartElement("guide")
    59.                     xwtr.WriteEndElement()
    60.  
    61.                     xwtr.WriteEndDocument()
    62.                     xwtr.Close()
    63.                     XMLInformation(mt, isbn, rating, age, anno, reviewer, page, title)
    64.                 End If
    65.             End Sub
    66.  
    67.             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)
    68.                 'Setup a new review element node
    69.                 Dim xReview As Xml.XmlElement = xdoc.CreateElement("review")
    70.                 xdoc.DocumentElement.AppendChild(xReview)
    71.                 'Add the attributes
    72.                 xReview.SetAttribute("mt", Convert.ToByte(mt).ToString.Trim)
    73.                 xReview.SetAttribute("rating", rating.Trim)
    74.                 xReview.SetAttribute("age", age)
    75.                 xReview.SetAttribute("reviewer", reviewer.Trim)
    76.  
    77.  
    78.                 'Setup a new isbns node
    79.                 Dim xIsbns As Xml.XmlElement = xdoc.CreateElement("isbns")
    80.                 For Each s As String In isbn.ToString.Split(";"c)
    81.                     Application.DoEvents()
    82.                     If s.Length <> 0 Then
    83.                         Dim xIsbn As Xml.XmlElement = xdoc.CreateElement("isbn")
    84.                         xIsbns.AppendChild(xIsbn)
    85.                         xIsbn.SetAttribute("number", s.Trim)
    86.                     End If
    87.                 Next
    88.                 xReview.AppendChild(xIsbns)
    89.  
    90.                 'Setup a new titles node
    91.                 Dim xTitles As Xml.XmlElement = xdoc.CreateElement("titles")
    92.                 Dim xTitle As Xml.XmlElement = xdoc.CreateElement("title")
    93.                 xTitles.AppendChild(xTitle)
    94.                 xTitle.SetAttribute("value", title.Trim)
    95.                 xTitle.SetAttribute("page_count", page.Trim)
    96.                 xReview.AppendChild(xTitles)
    97.  
    98.                 'Setup a new anno node
    99.                 Dim xAnno As Xml.XmlElement = xdoc.CreateElement("anno")
    100.                 'Setup a xml text object
    101.                 Dim xAnnoText As Xml.XmlText = xdoc.CreateTextNode(anno.Trim)
    102.                 xAnno.AppendChild(xAnnoText)
    103.                 xReview.AppendChild(xAnno)
    104.             End Sub

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  4. #4

    Thread Starter
    Fanatic Member jcavard's Avatar
    Join Date
    Jul 2005
    Location
    Quebec, CANADA
    Posts
    534

    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.
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

    accoustic emo-rock band: a tailormade fable

    Visual Studio 2003 / Framework 1.1

  5. #5
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    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.

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  6. #6

    Thread Starter
    Fanatic Member jcavard's Avatar
    Join Date
    Jul 2005
    Location
    Quebec, CANADA
    Posts
    534

    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??
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

    accoustic emo-rock band: a tailormade fable

    Visual Studio 2003 / Framework 1.1

  7. #7
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    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.

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  8. #8

    Thread Starter
    Fanatic Member jcavard's Avatar
    Join Date
    Jul 2005
    Location
    Quebec, CANADA
    Posts
    534

    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!
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

    accoustic emo-rock band: a tailormade fable

    Visual Studio 2003 / Framework 1.1

  9. #9
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    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

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  10. #10
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    Re: Create/Write XML File (Blank xml file created, nothing written)

    here's a solution for you

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim openFolder As New FolderBrowserDialog
    3.         Dim saveFile As New SaveFileDialog
    4.  
    5.         Dim sw As IO.StreamWriter
    6.  
    7.         Dim path As String
    8.         Dim file As String
    9.         Dim data As String
    10.         Dim images() As String
    11.  
    12.         Try
    13.             Button1.Enabled = False
    14.             openFolder.ShowDialog()
    15.  
    16.             saveFile.FileName = "*.xml"
    17.             saveFile.Filter = "XML File|*.xml"
    18.             saveFile.ShowDialog()
    19.  
    20.             If openFolder.SelectedPath <> "" Then
    21.                 path = openFolder.SelectedPath
    22.  
    23.                 If saveFile.FileName <> "" Then
    24.                     file = saveFile.FileName
    25.  
    26.                     images = IO.Directory.GetFiles(path)
    27.  
    28.                     'Create the xml file
    29.                     If Not IO.File.Exists(file) Then
    30.                         Dim xwtr As New Xml.XmlTextWriter(file, System.Text.Encoding.UTF8)
    31.  
    32.                         xwtr.Formatting = Xml.Formatting.Indented
    33.                         xwtr.Indentation = 4
    34.                         xwtr.QuoteChar = """"c
    35.                         xwtr.WriteStartDocument()
    36.                         'use whatever you want for the root node
    37.                         xwtr.WriteStartElement("ImageGallery")
    38.                         xwtr.WriteEndElement()
    39.  
    40.                         xwtr.WriteEndDocument()
    41.                         xwtr.Close()
    42.                     End If
    43.  
    44.                     '        data = "<images directory=""photos/Nuit de la Joke"">" + vbNewLine
    45.                     Dim xdoc As New Xml.XmlDocument
    46.  
    47.                     xdoc.Load(file)
    48.                     For i As Integer = 0 To images.Length - 1
    49.                         If IO.Path.GetExtension(images(i)) = ".jpg" Then
    50.                             'Setup a new image element node
    51.                             Dim xImage As Xml.XmlElement = xdoc.CreateElement("imageNode")
    52.                             xdoc.DocumentElement.AppendChild(xImage)
    53.                             'Add the attributes to the image node
    54.                             xImage.SetAttribute("jpegURL", IO.Path.GetFileName(images(i)))
    55.                             xImage.SetAttribute("thumbURL", IO.Path.GetFileName(images(i)))
    56.                             xImage.SetAttribute("title", "Title " + i.ToString)
    57.  
    58.                             'Save the new value to the xml file
    59.                             xdoc.Save(file)
    60.                         End If
    61.                     Next
    62.                     '        data += "</images>"
    63.  
    64.                     '        sw = New IO.StreamWriter(file)
    65.                     '        sw.Write(data)
    66.  
    67.                     '        MsgBox(data)
    68.                 End If
    69.             End If
    70.         Catch ex As Exception
    71.             MsgBox(ex.ToString)
    72.         Finally
    73.             Button1.Enabled = True
    74.         End Try
    75.  
    76.  
    77.     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>

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  11. #11

    Thread Starter
    Fanatic Member jcavard's Avatar
    Join Date
    Jul 2005
    Location
    Quebec, CANADA
    Posts
    534

    Re: Create/Write XML File (Blank xml file created, nothing written)

    hay! thank again man! really, thanks for your time! it works flawless
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

    accoustic emo-rock band: a tailormade fable

    Visual Studio 2003 / Framework 1.1

  12. #12
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    Re: Create/Write XML File (Blank xml file created, nothing written)

    np and anytime

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

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