Creating a file and writing to it

Posted on 2006-10-19
Last Modified: 2010-04-23
Whats the best way to create a file named "stuff.xml" then write these 2 lines:
<?xml version=""1.0""?>

then append these lines

Question by:npl77
  • 5
  • 2
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17767278
"best way" is subjective...

What version VB?
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17767311
VB.Net 2005:

        My.Computer.FileSystem.WriteAllText("c:\stuff.xml", "<?xml version=" & Chr(34) & Chr(34) & "1.0" & Chr(34) & Chr(34) & "?>" & vbCrLf & "<v></v>", False)
        My.Computer.FileSystem.WriteAllText("c:\stuff.xml", "<x></x>" & vbCrLf & "<y></>", True)
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17767355
That probably should be:

        My.Computer.FileSystem.WriteAllText("c:\stuff.xml", "<?xml version=" & Chr(34) & "1.0" & Chr(34) & "?>" & vbCrLf & "<v></v>" & vbCrLf, False)
        My.Computer.FileSystem.WriteAllText("c:\stuff.xml", "<x></x>" & vbCrLf & "<y></y>" & vbCrLf, True)
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17767381
For earlier .Net versions:

        Dim sw As New System.IO.StreamWriter("c:\stuff.xml", False)
        sw.Write("<?xml version=" & Chr(34) & "1.0" & Chr(34) & "?>" & vbCrLf & "<v></v>" & vbCrLf)

        Dim sw2 = New System.IO.StreamWriter("c:\stuff.xml", True)
        sw2.Write("<x></x>" & vbCrLf & "<y></y>" & vbCrLf)

Author Comment

ID: 17767405
i must be able to append on a button click append the( <x>, <y<>). I was using a streamwriter but it doesnt work

 xmlWriter = New StreamWriter(xmlPath)
 xmlWriter.Write("<?xml version=""1.0""?>" & vbCrLf )

then on the button click:


Author Comment

ID: 17767410
using vs2005
LVL 85

Accepted Solution

Mike Tomlinson earned 125 total points
ID: 17767450
From a couple comments ago:

    My.Computer.FileSystem.WriteAllText("c:\stuff.xml", "<x></x>" & vbCrLf & "<y></y>" & vbCrLf, True)

The last parameter specifices whether we should append or not.  False makes it overwrite the file.  True makes it append to the end of the file.

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net How to Exit Sub - Exit Form??? 5 59
Prevent call a sub/function several times when data bind to gridview 21 29
Regex validation 2 25
.NET 2008 VB and C# 6 28
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question