Reputation: 420
I have an xml file with structure like this:
<ArrayOfUser
<User>
<FirstName>John</FirstName>
</User>
</ArrayOfUser
I am attempting to grab information from a form and with it append to the xml file.
In my home controller I have this code:
[HttpPost]
public string writeMe()
{
string xmlFilePath = @"~App_Data/users.xml";
XmlDocument doc = new XmlDocument();
doc.Load(xmlFilePath);
XmlElement foo = doc.CreateElement("User");
XmlElement bar = doc.CreateElement("FirstName");
bar.InnerText = "Test";
foo.AppendChild(bar);
doc.DocumentElement.AppendChild(foo);
doc.Save(xmlFilePath);
return null;
}
My JS code is this (not currently passing anything for testing purposes):
var FirstName = $("input[name='FirstName']").val();
$.post("/Home/writeMe",
{},
function (response) {
alert("test");
}
);
I get the following error: "Could not find a part of the path 'C:\Program Files (x86)\IIS Express\~App_Data\users.xml'.'"
I have tried other paths such as ...App_Data\users.xml, etc to no avail. Besides this error I am not confident I am approaching this correctly as I am VERY new to using MVC and use to winforms. Any tips or help would be greatly appreciated.
Upvotes: 0
Views: 138
Reputation: 1917
Follow these two steps to fix your issue:
Server.MapPath("~/App_Data");
Upvotes: 1