How to add XML Declaration

G

Guest

Dear Pals,
I am creating a XML document through this code:

Imports System
Imports System.IO
Imports System.Xml
Imports System.Text

Module Module1
Sub Main()
Dim textWriter As New XmlTextWriter("e:\Emp.xml", _
System.Text.Encoding.UTF8)
textWriter.Formatting = Formatting.Indented
textWriter.WriteDocType("Employees", Nothing, Nothing, Nothing)
textWriter.WriteComment("This file represents a fragment of Employees" & _
"database")
textWriter.WriteStartElement("Employees")
textWriter.WriteStartElement("Employee", Nothing)
textWriter.WriteElementString("FirstName", "John")
textWriter.WriteElementString("LastName", "Doe")
textWriter.WriteElementString("DateOfBirth", "08/09/1968")
textWriter.WriteElementString("DateOfJoining", "04/01/1992")
textWriter.WriteEndElement()
textWriter.WriteEndElement()

' Write the XML to file and close the textWriter
textWriter.Flush()
textWriter.Close()
Console.WriteLine("Press a key to exit.")
Console.ReadLine()
End Sub
End Module

Here is my output:

<!DOCTYPE Employees>
<!--This file represents a fragment of Employeesdatabase-->
<Employees>
<Employee>
<FirstName>John</FirstName>
<LastName>Doe</LastName>
<DateOfBirth>08/09/1968</DateOfBirth>
<DateOfJoining>04/01/1992</DateOfJoining>
</Employee>
</Employees>

Can anyone tell me how to add XML Declaration at the top of the document.
Thanks in Advance....
 
G

Guest

Dear SamuelSudhakarJ,
Add this to your code..
textWriter.WriteStartDocument(True)

It will create
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
on the top of your XML document.

for further reference, visit this link.
ms-help://MS.NETFrameworkSDK/cpref/html/frlrfSystemXmlXmlTextWriterClassWriteStartDocumentTopic.htm
 

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