Click here to Skip to main content
15,847,077 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is my code to create xmlfile
when i'm writing into it for first time it's working fine.when i'm inserting the content again it showing error only one root node is allowed.
The problem is when i'm additing nodes again i need to append it as a child node to the root node but i don't know how to do it.
Please help me to do this
Sub CreateXmlFile()
       Dim objReportNodes As New System.Text.StringBuilder()
       Dim strDetails As String
       strDate = Request.Form("tBDate").ToString

       strTime = Request.Form("lBTime")

       strSubject = Request.Form("txtsubject")
       strAjanta = Request.Form("txtAjanta")
       strDetails = "<?xml version='1.0' encoding='windows-1252' ?>"
       strDetails &= "<Messages><Date>" & strDate & "</Date>" & _
                     "<Time>" & strTime & "</Time>" & _
                     "<Subject>" & strSubject & "</Subject>" & _
                     "<Ajanta>" & strAjanta & "</Ajanta>" & _

       Dim objFile As System.IO.TextWriter = System.IO.File.AppendText("D:\XMLFile3.xml")

       objFile = Nothing
       objReportNodes = Nothing
       Call ReadXmlFile()
   End Sub

i tried using AppendChildNode() but here i'm using textwriter so it is not converting to system.xml.xmlnode
Updated 27-Jun-12 22:57pm

As you're constructing your XML in a string, you can just use


Edit: Working solution
Dim objReportNodes As New Xml.XmlDocument
Dim strDetails As String

Dim strDate As Date ' = Request.Form("tBDate").ToString
Dim strTime As Date ' = Request.Form("lBTime")

Dim strSubject As String '= Request.Form("txtsubject")
Dim strAjanta As String ' = Request.Form("txtAjanta")
strDetails = "<Message><Date>" & strDate & "</Date>" & _
              "<Time>" & strTime & "</Time>" & _
              "<Subject>" & strSubject & "</Subject>" & _
              "<Ajanta>" & strAjanta & "</Ajanta>" & _

If IO.File.Exists("XMLFile3.xml") Then objReportNodes.Load("XMLFile3.xml") Else objReportNodes.LoadXml("<Messages></Messages>")
objReportNodes.FirstChild.InnerXml &= strDetails

Share this answer
Agustee 28-Jun-12 5:48am    
objReportNode is a stringbuilder we can't use loadxml here.
MarqW 28-Jun-12 5:52am    
My bad, I didn't read. I assumed it was an Xml.XmlDocument object (with the reference to Xml.XmlNode). Why not just use Xml.XmlDocument? It also has a save function for writing out to a text file. It actually lets you manipulate the Xml based on nodes too
Agustee 28-Jun-12 6:05am    
i tried using that it shows me multiple root element.
what i want when i'm adding the content again it should added under root node.
Is it possible to do this with textwriter?
MarqW 28-Jun-12 6:12am    
As indicated in Solution 2 - You need an a single root element (say called <messages>)
Then you can do XmlDoc.FirstChild.InnerXml &= strDetails (be sure to delete the line where you set "
Elangovan.N 28-Jun-12 6:13am    
it will show multiple root element error,
because you should have only one root node (all-message) as i said in my solution.
in your case 'message' is the root node, and your code is inserting same <message> root node while appending.
This is not correct.
First a fall, you have a mistake in xml formation.
The output of your xml looks like this.


Here xml will consider
as the parent node and if u inserted one more record, the same parent node is again created,so xml cannot create.

For this you need to have one parent and inside that sub- parent, your xml formation should be like this,

<messages id="0">
<messages id="1">

if you have your own unique id for
it will be useful,incase you printing only that required <message>.

Hope this link will help for your question:[^]
Share this answer
Agustee 28-Jun-12 7:59am    
thanq for the link it works.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900