Need help trying to create an XML file using VB .Net

G

Guest

I am just trying create and write to a xml file in my c: drive. I used two
online examples but they don't seem to work. The document is created but
when I open it, nothing is there. Can someone please tell me where I am
going wrong? Get this error:

Cannot view XML input using style sheet. Please correct the error and then
click the Refresh button, or try again later.
--------------------------------------------------------------------------------
XML document must have a top level element. Error processing resource
'file:///C:/xmltest.xml'.


Imports system
Imports System.Xml
Namespace WriteToXML

'This code will be used to create XML using VB .Net

Public Class CreateXMLDoc
Public Overloads Shared Function Main(ByVal args() As String)
As Integer

Try

'Creates the XML text writer to write to XML files and also
creates the document if it does not already exist
Dim writer As New XmlTextWriter("c:\\xmltest.xml", Nothing)

'Starts the new document
writer.WriteStartDocument()

'Add comments
writer.WriteComment("This is a test")
writer.WriteProcessingInstruction("Instruction", "Person
Record")

'Add elements
writer.WriteStartElement("p", "person", "urn:person")
writer.WriteStartElement("LastName", "")
writer.WriteString("Jenkins")
writer.WriteEndElement()

'Add another element
writer.WriteStartElement("FirstName", "")
writer.WriteString("Chris")
writer.WriteEndElement()

'Let's end this document
writer.WriteEndDocument()

'Catch all errors
Catch e As Exception

Console.WriteLine("Exception: {0}", e.ToString())
MsgBox(e)
End Try

End Function


End Class
End Namespace

thanks in advance
 
J

Jon Skeet [C# MVP]

OriginalStealth said:
I am just trying create and write to a xml file in my c: drive. I used two
online examples but they don't seem to work. The document is created but
when I open it, nothing is there. Can someone please tell me where I am
going wrong? Get this error:

Cannot view XML input using style sheet. Please correct the error and then
click the Refresh button, or try again later.

That's just IE being annoying. If you click on View Source, or open it
in Notepad, it should be fine.

(I haven't actually checked the code, I hasten to add...)
 
J

john

I am just trying create and write to a xml file in my c: drive. I used two
online examples but they don't seem to work. The document is created but
when I open it, nothing is there. Can someone please tell me where I am
going wrong? Get this error:

Cannot view XML input using style sheet. Please correct the error and then
click the Refresh button, or try again later.
---------------------------------------------------------------------------­-----
XML document must have a top level element. Error processing resource
'file:///C:/xmltest.xml'.

Imports system
Imports System.Xml
Namespace WriteToXML

'This code will be used to create XML using VB .Net

Public Class CreateXMLDoc
Public Overloads Shared Function Main(ByVal args() As String)
As Integer

Try

'Creates the XML text writer to write to XML files and also
creates the document if it does not already exist
Dim writer As New XmlTextWriter("c:\\xmltest.xml", Nothing)

'Starts the new document
writer.WriteStartDocument()

'Add comments
writer.WriteComment("This is a test")
writer.WriteProcessingInstruction("Instruction", "Person
Record")

'Add elements
writer.WriteStartElement("p", "person", "urn:person")
writer.WriteStartElement("LastName", "")
writer.WriteString("Jenkins")
writer.WriteEndElement()

'Add another element
writer.WriteStartElement("FirstName", "")
writer.WriteString("Chris")
writer.WriteEndElement()

'Let's end this document
writer.WriteEndDocument()

writer.Flush()
writer.Close()

'Catch all errors
Catch e As Exception

Console.WriteLine("Exception: {0}", e.ToString())
MsgBox(e)
End Try

End Function

End Class
End Namespace

thanks in advance
 
R

Rich

- Show quoted text -- Hide quoted text -

- Show quoted text -

I think I follow the thread OK . . . .

BeginNameSpace
NewClass
Function

End Function
EndClass
EndNameSpace

But how do I direct the action to the namespace, and thus create the
xml?

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top