Click here to Skip to main content
12,503,829 members (40,314 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB XML VB.NET
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>" & _
                     "</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 27-Jun-12 21:41pm
Agustee826
Updated 27-Jun-12 21:57pm
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

objReportNodes.LoadXml(strDetails)
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>" & _
              "</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")
  Permalink  
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
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
 
good
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.

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

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

<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
<message></message>
it will be useful,incase you printing only that required



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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160919.1 | Last Updated 28 Jun 2012
Copyright © CodeProject, 1999-2016
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