Results 1 to 9 of 9

Thread: unable to write to file

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2005
    Posts
    38

    unable to write to file

    Hi..
    I am trying to create a xml file and write some information into the xml file but when i open the xml file i don't have any information.
    Even if i try to create a txt file and do the same thing but it is still the same.
    i am not sure why..
    can someone help me...

    VB Code:
    1. Dim path As String = "School"
    2.         Dim target As String = "School/latcomers.txt"
    3.  
    4.         Try
    5.             If Directory.Exists(path) = False Then
    6.                 Directory.CreateDirectory(path)
    7.                 Dim sw As StreamWriter = File.CreateText(target)
    8.                 sw.Write(("<info class=""1A"" indexno=""01"" date=""10/10/2005"" time=""10:18:00""/>"))
    9.  
    10.             ElseIf Directory.Exists(path) = True Then
    11.                 Dim sw As StreamWriter = File.CreateText(target)
    12.                 sw.Write("<info class=""1A"" indexno=""01"" date=""10/10/2005"" time=""10:18:00""/>")
    13.             End If
    14.  
    15.         Catch ex As Exception
    16.             MsgBox("unable to create")
    17.         End Try
    Thanxs......

  2. #2
    Fanatic Member Strider's Avatar
    Join Date
    Sep 2004
    Location
    Dublin, Ireland
    Posts
    612

    Re: unable to write to file

    Why are you creating XML file with the streamwriter.
    Have you looked at System.XML and the XMLWriter???? this is really what you should use for creating xml files



    however to create and write to file:

    VB Code:
    1. Dim l_sWriter As New StreamWriter(target)
    2. Dim l_writeLine As String
    3. l_writeLine = "<info class=""1A"" indexno=""01"" date=""10/10/2005"" time=""10:18:00""/>"
    4.  
    5. l_sWriter.WriteLine(l_writeLine)
    6. l_sWriter.Close()
    Barry


    Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
    .NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0

    SQL Server 2005/2000/SQL Server CE 2.0


    If you like, rate this post

    Compact Framework for Beginners

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2005
    Posts
    38

    Re: unable to write to file

    ya.
    I tried using the xmltextwriter, but i am very cofused because when I add the codes:

    Dim w As XmlTextWriter = New XmlTextWriter(target, )

    in the brackets (target is my filename, after the commer as stated in the constructor there shlould be (encoding As System.text.Encoding) but i don't need that)
    So, i am not sure how to place the codes such that it will write to my xml file using the xmlwriter or xmltextwriter....

    Thanxs 4 helping...

  4. #4
    Fanatic Member Strider's Avatar
    Join Date
    Sep 2004
    Location
    Dublin, Ireland
    Posts
    612

    Re: unable to write to file

    there is no need for encoding, all you need to do is pass "Nothing" into it.

    here is a sample
    VB Code:
    1. Try
    2.             Dim l_xmlWriter As New XmlTextWriter(p_file, Nothing)
    3.  
    4.             l_xmlWriter.Indentation = 1
    5.             l_xmlWriter.Formatting = Formatting.Indented
    6.             l_xmlWriter.WriteStartDocument(True)
    7.             l_xmlWriter.WriteComment("Created: " & Date.Now.ToString)
    8.  
    9.             l_xmlWriter.WriteStartElement("Config")
    10.  
    11.             l_xmlWriter.WriteStartElement("DeviceLocations")
    12.             l_xmlWriter.WriteStartElement("HHImport")
    13.             l_xmlWriter.WriteAttributeString("Value", p_settings.HandheldImportDirectory)
    14.             l_xmlWriter.WriteEndElement()
    15.  
    16.             l_xmlWriter.WriteStartElement("HHExport")
    17.             l_xmlWriter.WriteAttributeString("Value", p_settings.HandheldExportDirectory)
    18.             l_xmlWriter.WriteEndElement()
    19.  
    20.             l_xmlWriter.WriteEndElement() 'End Device Locations
    21.  
    22.             l_xmlWriter.WriteStartElement("ServerLocations")
    23.             l_xmlWriter.WriteStartElement("PCImport")
    24.             l_xmlWriter.WriteAttributeString("Value", p_settings.PCImportDirectory)
    25.             l_xmlWriter.WriteEndElement()
    26.  
    27.             l_xmlWriter.WriteStartElement("PCExport")
    28.             l_xmlWriter.WriteAttributeString("Value", p_settings.PCExportDirectory)
    29.             l_xmlWriter.WriteEndElement()
    30.  
    31.             l_xmlWriter.WriteEndElement() 'End Server Locations
    32.  
    33.            
    34.             l_xmlWriter.WriteEndDocument() 'end node
    35.  
    36.             l_xmlWriter.Close()
    37.         Catch ex As IO.IOException
    38.             Throw New Exception(ex.Message)
    39.         Catch ex As XmlException
    40.             Throw New Exception(ex.Message)
    41.         Catch ex As Exception
    42.             Throw New Exception(ex.Message)
    43.         End Try
    Barry


    Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
    .NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0

    SQL Server 2005/2000/SQL Server CE 2.0


    If you like, rate this post

    Compact Framework for Beginners

  5. #5
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: unable to write to file

    Why have you got so many catch in there?

    you trying to catch a fish?
    If you find my thread helpful, please remember to rate me

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2005
    Posts
    38

    Re: unable to write to file

    ok..thanxs 4 the sample codes..i got the picture of how it works...

  7. #7
    Fanatic Member Strider's Avatar
    Join Date
    Sep 2004
    Location
    Dublin, Ireland
    Posts
    612

    Re: unable to write to file

    Quote Originally Posted by dinosaur_uk
    Why have you got so many catch in there?

    you trying to catch a fish?
    sorry dude, must have got carried away

    its a bit of overdosing on catches alright
    Barry


    Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
    .NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0

    SQL Server 2005/2000/SQL Server CE 2.0


    If you like, rate this post

    Compact Framework for Beginners

  8. #8
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: unable to write to file

    lol....

    anyway.....isn't try and catch strongly discouraged because it uses up alot of resources?
    If you find my thread helpful, please remember to rate me

  9. #9
    Fanatic Member Strider's Avatar
    Join Date
    Sep 2004
    Location
    Dublin, Ireland
    Posts
    612

    Re: unable to write to file

    never knew that is used up so much resources i've seen how something it can take a while for it to throw the exception alright


    i generally use then a lot with files because i always find that there is always some error with some data in them

    really there should only be one catch in the code above....
    Barry


    Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
    .NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0

    SQL Server 2005/2000/SQL Server CE 2.0


    If you like, rate this post

    Compact Framework for Beginners

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