Saturday, April 30, 2011

Only one top level element is allowed in an XML document

hi guys, Following is the code for inserting new nodes in an xml file .

Dim fleStream As New FileStream("C:\mailing.xml", FileMode.Append, FileAccess.Write,FileShare.ReadWrite)
Dim stmWriter As New StreamWriter(fleStream)
Dim writer As New XmlTextWriter(stmWriter)
Dim currNode As XmlNode
Dim doc As New XmlDocument

doc.LoadXml(("<XMLFile>" + " <EMail></EMail>" + "</XMLFile>"))
'doc.Load("C:\mailing.xml") '
Dim docFrag As XmlDocumentFragment = doc.CreateDocumentFragment()
docFrag.InnerXml = "<From><Address>" + txtFrom.Text + " </Address></From>"
currNode = doc.DocumentElement.FirstChild.AppendChild(docFrag)
currNode.InsertAfter(docFrag, currNode.LastChild)

docFrag.InnerXml = "<Receipient> <To>" + txtTo.Text + " </To></Receipient>"
currNode = doc.DocumentElement.FirstChild.FirstChild.AppendChild(docFrag)
currNode.InsertAfter(docFrag, currNode.LastChild)

docFrag.InnerXml = "<Subject>" + txtSubject.Text + "</Subject>"
currNode = doc.DocumentElement.FirstChild.AppendChild(docFrag)
currNode.InsertAfter(docFrag, currNode.LastChild)

docFrag.InnerXml = "<Body>" + txtBody.Text + "</Body>"
currNode = doc.DocumentElement.FirstChild.AppendChild(docFrag)
currNode.InsertAfter(docFrag, currNode.LastChild)

doc.Save(writer)
'doc.Save("C:\xmlmailfile.xml") '
writer.Flush()
stmWriter.Flush()
stmWriter.Close()

By executing this i cant able to view the result in IE,an error msg comes as "The XML page cannot be displayed

Cannot view XML input using XSL style sheet.

Only one top level element is allowed in an XML document. Error processing resource 'file:///C:/mailing.xml'. Line 2, Posi...

asdasd@qwe.com

When i open the xml file the datas are entered,Can anyone help me to solve the pblm.What modifications are to be done in my code?

From stackoverflow
  • Open it in Notepad and see what the output is, and from there see what the error really is, and post it here if you need more help.

  • Well, this is quite normal. An XML document must have a root element that is not duplicated. You have already asked the related question here and originally here. Jon Skeet quite succinctly explained this to you already.

    If there is an issue in understanding, you should consider reading up on XML or doing some research, instead of asking the same question repeatedly because most people will not realize that you have already received excellent answers to the problem.

    If you keep doing the same thing you've already done, you should not expect different results.

0 comments:

Post a Comment