all groups > dotnet xml > august 2005 >
You're in the

dotnet xml

group:

The root element is missing


The root element is missing Samem N via DotNetMonster.com
8/12/2005 7:29:21 PM
dotnet xml:
Does anyone know how to solve this error? I dont know where it went wrong.
Any help would be appreciated .Thanks

ERROR
______

Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.Xml.XmlException: The root element is missing.

Source Error:


Line 81: // Load the xml file
Line 82: XmlDocument xmldoc = new XmlDocument();
Line 83: xmldoc.Load( Server.MapPath("guestbook.xml"));
Line 84:
Line 85: //Create a new guest element and add it to the root node


Source File: e:\year 3\rhythm\rhythm_fyp\postcomments.aspx.cs Line: 83

Stack Trace:


[XmlException: The root element is missing.]
System.Xml.XmlTextReader.Read() +876
System.Xml.XmlValidatingReader.ReadWithCollectTextToken()
System.Xml.XmlValidatingReader.Read()
System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean
preserveWhitespace) +80
System.Xml.XmlDocument.Load(XmlReader reader) +72
System.Xml.XmlDocument.Load(String filename)
Rhythm_Fyp.PostComments.SaveXMLData() in e:\year 3\rhythm\rhythm_fyp\
postcomments.aspx.cs:83
Rhythm_Fyp.PostComments.btnPost_Click(Object sender, EventArgs e) in e:\
year 3\rhythm\rhythm_fyp\postcomments.aspx.cs:71
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.
RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,
String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
Re: The root element is missing Pascal Schmitt
8/13/2005 12:00:00 AM
Hello!

[quoted text, click to view]

Well, it seems your guestbook.xml is invalid - maybe you could post it
here? Change the code to this:

XmlDocument xmldoc = new XmlDocument();
string filename = Server.MapPath("guestbook.xml");
Trace.Write( filename );
xmldoc.Load( filename );

Does the file really exist? Does it contain any elements? Did you create
it by concatting multiple XML-Files?


--
Re: The root element is missing Samem N via DotNetMonster.com
8/13/2005 12:00:00 AM
Again, its says " The root element is missing"

I took this code from the net, but it doesnt seem to work for me. The XML
file has not been created and no elements are inside.

How do u think i can solve this?

Here's the original code
--------------------------------

01: using System;
02: using System.Web;
03: using System.Web.UI;
04: using System.Web.UI.WebControls;
05: using System.Xml;
06: public class Guestbook : Page
07: {
08: // Create the required webcontrols with the same name as in the
guestbook.aspx 09: //file.
10: public TextBox name;
11: public TextBox location;
12: public TextBox email;
13: public TextBox website;
14: public TextBox comment;
15: public void Save_Comment(object sender, EventArgs e)
16: {
17: // Everything is all right, so let us save the data into the XML file
18: SaveXMLData();
19: // Remove the values of the textboxes
20: name.Text="";
21: location.Text="";
22: website.Text="";
23: email.Text="";
24: comment.Text="";
25: }
26: }
27: private void SaveXMLData()
28: {
29: // Load the xml file
30: XmlDocument xmldoc = new XmlDocument();
31: xmldoc.Load( Server.MapPath("guestbook.xml") );
32: //Create a new guest element and add it to the root node
33: XmlElement parentNode = xmldoc.CreateElement("guest");
34: xmldoc.DocumentElement.PrependChild(parentNode);
35: // Create the required nodes
36: XmlElement nameNode = xmldoc.CreateElement("name");
37: XmlElement locationNode = xmldoc.CreateElement("location");
38: XmlElement emailNode = xmldoc.CreateElement("email");
39: XmlElement websiteNode = xmldoc.CreateElement("website");
40: XmlElement commentNode = xmldoc.CreateElement("comment");
41: // retrieve the text
42: XmlText nameText = xmldoc.CreateTextNode(name.Text);
43: XmlText locationText = xmldoc.CreateTextNode(location.Text);
44: XmlText emailText = xmldoc.CreateTextNode(email.Text);
45: XmlText websiteText = xmldoc.CreateTextNode(website.Text);
46: XmlText commentText = xmldoc.CreateTextNode(comment.Text);
47: // append the nodes to the parentNode without the value
48: parentNode.AppendChild(nameNode);
49: parentNode.AppendChild(locationNode);
50: parentNode.AppendChild(emailNode);
51: parentNode.AppendChild(websiteNode);
52: parentNode.AppendChild(commentNode);
53: // save the value of the fields into the nodes
54: nameNode.AppendChild(nameText);
55: locationNode.AppendChild(locationText);
56: emailNode.AppendChild(emailText);
57: websiteNode.AppendChild(websiteText);
58: commentNode.AppendChild(commentText);
59: // Save to the XML file
60: xmldoc.Save( Server.MapPath("guestbook.xml") );
61: // Display the user the signed guestbook
62: Response.Redirect("viewguestbook.aspx");
63: }
64: }


Source :
http://www.devarticles.com/c/a/ASP.NET/Create-Your-Own-Guestbook-In-ASP.NET/1/



--
Re: The root element is missing Pascal Schmitt
8/13/2005 7:58:26 PM
Hello!

[quoted text, click to view]

You have to create the guestbook.xml file yourself, I think this should
work:

<?xml version="1.0" ?>
<guestbook>
</guestbook>


--
AddThis Social Bookmark Button