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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
LVL 86

Expert Comment

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

What version VB?
LVL 86

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 86

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)
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 86

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 86

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

733 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