Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Christian Kragh 14 posts 34 karma points
    Jun 10, 2011 @ 14:24
    Christian Kragh
    0

    XML log.

    I try to make a comment page, and i used a XML log to store the data.

    But the script makes 2 equal comment in the log file.

    Can anybody see what is wrong?

    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Net;
    using System.Net.Mail;
    using System.Net.Configuration;
    using System.Text.RegularExpressions;
    using System.Configuration;
    using System.Web.Configuration;
    using System.Xml;
    using System.Text;
    using System.IO;
    using umbraco;
    using umbraco.presentation.nodeFactory;

    namespace umbracowebsitewizard_site.Usercontrols {
    public partial class ContactForm : System.Web.UI.UserControl {
    public void btnSubmit_Click(object sender, EventArgs e) {
    if (!Page.IsValid) { return; }

    string strName, strMessage;
    strName = name.Text;
    strMessage = message.Text;
    int document = umbraco.presentation.nodeFactory.Node.GetCurrent().Id;

    string strEmailFrom, strEmailTo, strEmailSubject, strEmailBody, strTime, strDate;
    strEmailFrom = emailAddress.Text;
    strEmailTo = "[email protected]";
    strEmailSubject = "Kommentar tilføjet til" + document;
    strTime = String.Format("{0:HH:mm:ss}", DateTime.Now);
    strDate = String.Format("{0:dd/MM/yyyy}", DateTime.Now);

    strEmailBody = "Der er kommen en kommentar til dit din side fra:" + Environment.NewLine + "Navn: " + strName + "." + Environment.NewLine + "Tidspunkt: " + strDate + " " + strTime + Environment.NewLine + ".Email: " + strEmailFrom + "." + Environment.NewLine + "Meddelelse: " + strMessage;

    SmtpClient mySMTPClient = new SmtpClient();

    try {
    mySMTPClient.Send(strEmailFrom, strEmailTo, strEmailSubject, strEmailBody);
    }
    catch (SmtpException ex) {
    errorMessage.Text = ex.Message;
    ErrorMailSettings.Visible = true;
    FormFields.Visible = true;
    return;
    }

    string filePath = Server.MapPath("~/log_files/comment_" + document + ".xml");

    if (!File.Exists(filePath)) {
    XmlTextWriter writer = new XmlTextWriter(filePath, Encoding.UTF8);
    writer.Formatting = Formatting.Indented;
    writer.WriteStartDocument();
    writer.WriteStartElement("logs");
    writer.WriteStartElement("log");
    writer.WriteAttributeString("time", strTime);
    writer.WriteAttributeString("date", strDate);
    writer.WriteAttributeString("accepted", "false");

    writer.WriteStartElement("field");
    writer.WriteAttributeString("alias", "name");
    writer.WriteString(strName);
    writer.WriteEndElement();

    writer.WriteStartElement("field");
    writer.WriteAttributeString("alias", "email");
    writer.WriteString(strEmailFrom);
    writer.WriteEndElement();

    writer.WriteStartElement("field");
    writer.WriteAttributeString("alias", "message");
    writer.WriteCData(strMessage);
    writer.WriteEndElement();

    writer.WriteEndElement();
    writer.WriteEndDocument();
    writer.Close();
    } else {
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(filePath);
    XmlElement logNode = xmlDoc.CreateElement("log");
    logNode.SetAttribute("time", strTime);
    logNode.SetAttribute("date", strDate);
    logNode.SetAttribute("accepted", "false");

    XmlElement fieldNodeName = xmlDoc.CreateElement("field");
    fieldNodeName.SetAttribute("alias", "name");
    fieldNodeName.InnerText = strName;
    logNode.AppendChild(fieldNodeName);

    XmlElement fieldNodeEmail = xmlDoc.CreateElement("field");
    fieldNodeEmail.SetAttribute("alias", "email");
    fieldNodeEmail.InnerText = strEmailFrom;
    logNode.AppendChild(fieldNodeEmail);

    XmlElement fieldNodeMessage = xmlDoc.CreateElement("field");
    fieldNodeMessage.SetAttribute("alias", "message");
    XmlCDataSection fieldNodeCData = xmlDoc.CreateCDataSection(strMessage);
    fieldNodeMessage.AppendChild(fieldNodeCData);
    logNode.AppendChild(fieldNodeMessage);

    xmlDoc.SelectSingleNode("/logs").AppendChild(logNode);
    xmlDoc.Save(filePath);
    }

    FormFields.Visible = false;
    ErrorMailSettings.Visible = false;
    ThankYou.Visible = true;
    }

    protected void RequiredName_ServerValidate(object source, ServerValidateEventArgs args) { if (name.Text == String.Empty) { name.CssClass = "error"; args.IsValid = false; }else{ name.CssClass = ""; args.IsValid = true;}}
    protected void RequiredEmailAddress_ServerValidate(object source, ServerValidateEventArgs args) {if (emailAddress.Text == String.Empty) {emailAddress.CssClass = "error";args.IsValid = false;}else{emailAddress.CssClass = "";args.IsValid = true;}}
    protected void RequiredMessage_ServerValidate(object source, ServerValidateEventArgs args) {if (message.Text == String.Empty) {message.CssClass = "error";args.IsValid = false;}else{message.CssClass = "";args.IsValid = true;}}
    protected void CheckEmailAddress_ServerValidate(object source, ServerValidateEventArgs args) {Regex reg = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");if (!reg.IsMatch(emailAddress.Text)) {emailAddress.CssClass = "error";args.IsValid = false;} else {emailAddress.CssClass = "";args.IsValid = true;}}
    }
    }

Please Sign in or register to post replies

Write your reply to:

Draft