Groups | Blog | Home
all groups > dotnet xml > september 2004 >

dotnet xml : Newbie in XML with C# question


Aaron
9/17/2004 11:34:08 PM
Hello,

I am learning how to using XML documents to store data.

I get this error:
Access to the path "c:\inetpub\wwwroot\Info.xml" is denied.

I have tried with the full path of the file, with / infront of the file.

How do I get the data in too the xml document


Here is the code below:
using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.Xml;

using System.Xml.XPath;

using System.Xml.Xsl;

namespace xDoc

{

/// <summary>

/// Summary description for WebForm1.

/// </summary>

public class WebForm1 : System.Web.UI.Page

{

protected System.Web.UI.WebControls.Label Label1;

protected System.Web.UI.WebControls.TextBox txtName;

protected System.Web.UI.WebControls.Label Label2;

protected System.Web.UI.WebControls.TextBox txtEmail;

protected System.Web.UI.WebControls.Label Label3;

protected System.Web.UI.WebControls.TextBox txtComments;

protected System.Web.UI.WebControls.Button btnSubmit;

protected System.Web.UI.WebControls.Button btnClear;

protected System.Web.UI.WebControls.Label Label4;


private void Page_Load(object sender, System.EventArgs e)

{

// Put user code to initialize the page here

}

#region Web Form Designer generated code

override protected void OnInit(EventArgs e)

{

//

// CODEGEN: This call is required by the ASP.NET Web Form Designer.

//

InitializeComponent();

base.OnInit(e);

}


/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

private void btnSubmit_Click(object sender, System.EventArgs e)

{

try

{

XmlTextWriter xDoc = new XmlTextWriter(Request.MapPath("Info.xml"),

System.Text.Encoding.Default);

xDoc.Formatting = Formatting.Indented;

xDoc.WriteStartDocument();

xDoc.WriteComment("2004 eXQue");


xDoc.WriteStartElement("Comments");

xDoc.WriteStartElement("Name");

xDoc.WriteString(txtName.Text);

xDoc.WriteEndElement();

xDoc.WriteStartElement("Email");

xDoc.WriteString(txtEmail.Text);

xDoc.WriteEndElement();

xDoc.WriteStartElement("Comments");

xDoc.WriteString(txtComments.Text);

xDoc.WriteEndElement();

xDoc.WriteEndDocument();


xDoc.Close();

Label4.Text = "Done";

}

catch (Exception ex)

{

Label4.Text = ex.Message.ToString();

}

}

}

}





TIA



Aaron

Derek Harmon
9/18/2004 3:14:18 PM
[quoted text, click to view]

You need to grant the ASPNET worker process user account
privileges to read/write/modify files in the \inetpub\wwwroot
directory (this paints the solution with a bit of a broad brush,
but it's also the simplest answer).

In Windows Explorer, navigate to that directory, right-click on
it, select Properties, Security tab, etc. Make sure that the
ASPNET user has read/write/modify rights, if not Add the
user account (if you're running Windows 2003 Server and
IIS 6.0, the user is named NetworkService).

A parting word on security -- please look into writing least-
privileged apps and use impersonation to a different user
account for your web application than the default ASPNET
account, and then set very constrained privileges on that
application user account so it can only write files off in
some application subdirectory (not wwwroot). I know this
is probably just a learning sample app, and I offer the
simple answer alongside this caveat that determining
the proper security policy for a production app isn't as
simple.


Derek Harmon

Aaron
9/18/2004 3:45:29 PM
Derek,
Thanks

Aaron
[quoted text, click to view]

AddThis Social Bookmark Button