|
-
Sep 13th, 2005, 02:50 AM
#1
Thread Starter
Member
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:
Dim path As String = "School"
Dim target As String = "School/latcomers.txt"
Try
If Directory.Exists(path) = False Then
Directory.CreateDirectory(path)
Dim sw As StreamWriter = File.CreateText(target)
sw.Write(("<info class=""1A"" indexno=""01"" date=""10/10/2005"" time=""10:18:00""/>"))
ElseIf Directory.Exists(path) = True Then
Dim sw As StreamWriter = File.CreateText(target)
sw.Write("<info class=""1A"" indexno=""01"" date=""10/10/2005"" time=""10:18:00""/>")
End If
Catch ex As Exception
MsgBox("unable to create")
End Try
Thanxs......
-
Sep 13th, 2005, 03:28 AM
#2
Fanatic Member
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:
Dim l_sWriter As New StreamWriter(target)
Dim l_writeLine As String
l_writeLine = "<info class=""1A"" indexno=""01"" date=""10/10/2005"" time=""10:18:00""/>"
l_sWriter.WriteLine(l_writeLine)
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
-
Sep 14th, 2005, 12:01 AM
#3
Thread Starter
Member
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...
-
Sep 14th, 2005, 03:08 AM
#4
Fanatic Member
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:
Try
Dim l_xmlWriter As New XmlTextWriter(p_file, Nothing)
l_xmlWriter.Indentation = 1
l_xmlWriter.Formatting = Formatting.Indented
l_xmlWriter.WriteStartDocument(True)
l_xmlWriter.WriteComment("Created: " & Date.Now.ToString)
l_xmlWriter.WriteStartElement("Config")
l_xmlWriter.WriteStartElement("DeviceLocations")
l_xmlWriter.WriteStartElement("HHImport")
l_xmlWriter.WriteAttributeString("Value", p_settings.HandheldImportDirectory)
l_xmlWriter.WriteEndElement()
l_xmlWriter.WriteStartElement("HHExport")
l_xmlWriter.WriteAttributeString("Value", p_settings.HandheldExportDirectory)
l_xmlWriter.WriteEndElement()
l_xmlWriter.WriteEndElement() 'End Device Locations
l_xmlWriter.WriteStartElement("ServerLocations")
l_xmlWriter.WriteStartElement("PCImport")
l_xmlWriter.WriteAttributeString("Value", p_settings.PCImportDirectory)
l_xmlWriter.WriteEndElement()
l_xmlWriter.WriteStartElement("PCExport")
l_xmlWriter.WriteAttributeString("Value", p_settings.PCExportDirectory)
l_xmlWriter.WriteEndElement()
l_xmlWriter.WriteEndElement() 'End Server Locations
l_xmlWriter.WriteEndDocument() 'end node
l_xmlWriter.Close()
Catch ex As IO.IOException
Throw New Exception(ex.Message)
Catch ex As XmlException
Throw New Exception(ex.Message)
Catch ex As Exception
Throw New Exception(ex.Message)
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
-
Sep 14th, 2005, 03:48 PM
#5
Frenzied Member
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 
-
Sep 14th, 2005, 09:20 PM
#6
Thread Starter
Member
Re: unable to write to file
ok..thanxs 4 the sample codes..i got the picture of how it works...
-
Sep 15th, 2005, 05:35 AM
#7
Fanatic Member
Re: unable to write to file
 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
-
Sep 15th, 2005, 06:09 AM
#8
Frenzied Member
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 
-
Sep 15th, 2005, 08:33 AM
#9
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|