• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

Creating a file and writing to it

Whats the best way to create a file named "stuff.xml" then write these 2 lines:
<?xml version=""1.0""?>
<v></v>

then append these lines
<x></x>
<y></>

0
npl77
Asked:
npl77
  • 5
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"best way" is subjective...

What version VB?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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)
        sw.Close()

        Dim sw2 = New System.IO.StreamWriter("c:\stuff.xml", True)
        sw2.Write("<x></x>" & vbCrLf & "<y></y>" & vbCrLf)
        sw2.Close()
0
 
npl77Author Commented:
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:

xmlWriter.Write(xml)
0
 
npl77Author Commented:
using vs2005
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now