Click here to Skip to main content
13,253,118 members (50,680 online)
Rate this:
Please Sign up or sign in to 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
Posted 27-Jun-12 22:41pm
Updated 27-Jun-12 22:57pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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
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.
Agustee 28-Jun-12 8:02am
i used XPathNavigator to move to child node
MarqW 28-Jun-12 8:07am
You don't even use XmlDocument in that function, how can you use XPathNavigator?

Sounds like you're working in a different area to what you're posting your question on.
Agustee 29-Jun-12 5:20am
sry i have changed my code later, if file exist i used xmldocument to load my xml file then i used xpath. i'm new to xml tats y this problem.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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:[^]
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.171114.1 | Last Updated 28 Jun 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100