0

I have an XML document in my project that looks like this

<?xml version="1.0" encoding="utf-8" ?>
<event>
  <name>Test Event</name>
  <date>06/19/1967</date>
  <description>Birthday</description>
  <blogURL></blogURL>
</event>

I want to create a webpage that accepts user input and replaces these values.

Here is my aspx page

<asp:Panel ID="NewEvent" CssClass="eventSection" runat="server" Visible="false">
    <asp:Label runat="server" ID="MessageBox" Text="" Visible="false"></asp:Label>
    <telerik:RadTextBox ID="NewEventName" placeholder="Event Name" runat="server"></telerik:RadTextBox>
    <br />
    <telerik:RadDatePicker ID="NewDate" runat="server"></telerik:RadDatePicker>
    <br />
    <telerik:RadTextBox ID="NewDescription" runat="server" placeholder="Description of event" TextMode="MultiLine"></telerik:RadTextBox>
    <br />
    <telerik:RadTextBox ID="NewURL" runat="server" placeholder="Leave blank if none"></telerik:RadTextBox>
    <br />
    <telerik:RadButton ID="EditXML" runat="server" Text="Submit Event" OnClick="EditXML_Click"></telerik:RadButton>
</asp:Panel>

My code behind to grab these values

    string newEventName = NewEventName.Text;
    string newEventDescription = NewDescription.Text;
    string newEventDate = NewDate.SelectedDate.Value.Date.ToString();
    string newEventURL = NewURL.Text;

And finally, actually inserting values into xml document

    XDocument doc = XDocument.Load("/PressSection.xml");
    doc.Element("name").Value = newEventName;
    doc.Element("date").Value = newEventDate;
    doc.Element("description").Value = newEventDescription;
    doc.Element("blogURL").Value = newEventURL;
    doc.Save(Server.MapPath("~/PressSection.xml"));

The XML file is located in the same directory as my aspx file

Image

What I have tried

    string path = Server.MapPath("PressSection.xml");
    XDocument doc = XDocument.Load(path);

which gives this error

error

Which I'm not sure why. When I set a break point and hover over my values, they are filled.

I've also tried

    XDocument doc = XDocument.Load(Server.MapPath("~/PressSection.xml"));

Which gives me the same error as above.

Finally, I've also tried

XDocument doc = XDocument.Load("\\PressSection.xml");

Which gives me this error

final image

I don't know what to do at this point.

halfer
  • 19,824
  • 17
  • 99
  • 186
onTheInternet
  • 6,421
  • 10
  • 41
  • 74
  • 1
    NRE is likely usual duplicate of http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it... You may want to start with solving that error. – Alexei Levenkov Aug 08 '16 at 20:04
  • Unless you are re-throwing an exception that occured elsewhere, the `exception` you are getting is very clear, `NewEventName` must be `null` when you try to call `.Text` on it. – starlight54 Aug 08 '16 at 20:04

1 Answers1

0

Element returns the first child element of the current container (element / document) only. So each of your queries like this one:

doc.Element("name").Value = newEventName;

Will return null, and your subsequent access of Value will result in a null reference exception. You should get the event element first:

var event = doc.Element("event");
event.Element("name").Value = newEventName;
Charles Mager
  • 25,735
  • 2
  • 35
  • 45