Click here to Skip to main content
15,884,836 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
VB
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>" & _
                     "</Messages>"
       objReportNodes.Append(strDetails)

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

       objFile.WriteLine(objReportNodes)
       objFile.Close()
       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
Posted
Updated 27-Jun-12 21:57pm
v3

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

objReportNodes.LoadXml(strDetails)

Edit: Working solution
VB
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>" & _
              "</Message>"

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

objReportNodes.Save("XMLFile3.xml")
 
Share this answer
 
v2
Comments
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    
@Agustee
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.

XML
<messages>
    <date>strDate</date>
    <time>strTime</time>
    <subject>strSubject</subject>
    <ajanta>strAjanta</ajanta>
</messages>


Here xml will consider
XML
<message></message>
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,

XML
<all-message>
<messages id="0">
    <date>strDate</date>
    <time>strTime</time>
    <subject>strSubject</subject>
    <ajanta>strAjanta</ajanta>
</messages>
<messages id="1">
    <date>strDate1</date>
    <time>strTime1</time>
    <subject>strSubject1</subject>
    <ajanta>strAjanta1</ajanta>
</messages>
</all-message>


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



Hope this link will help for your question:
http://msdn.microsoft.com/en-us/library/5x8bxy86(v=vs.80).aspx[^]
 
Share this answer
 
v2
Comments
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