Matt Rowles
Matt Rowles

Reputation: 8050

aspx Form to XML File

Basically what I am trying to is:

  1. Catch a form submission;
  2. Save it to an XML file;
  3. Send it to another server.

My main issue is not being able to find much information about XMLBuilder. This following link looks like something I need, but I can only use XML Builder: Creating a Contact form in Visual Studio ASPX and saving to an XML file when clicking SUBMIT

My code is as follows:

Default.aspx:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="ToXMLApp.ToXMLForm" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>

    <form id="ToXMLForm" runat="server" defaultbutton="Submit">
    <div>
        <asp:Label runat="server">Firstname</asp:Label>&emsp;<asp:TextBox ID="Firstname" runat="server"></asp:TextBox><br />
        <asp:Label runat="server">Surname</asp:Label>&emsp;<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        <asp:Button ID="Submit" runat="server" Text="Submit" OnClick="Submit_Click" />
    </div>
    </form>

</body>
</html>

Default.aspx.vb:

Protected Sub Submit_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    // XMLBuilder new xml file
    // save form results as xml fields
    // save xml file
    // send XML file (this is not as important atm)
    ToXMLForm.InnerHTML? // how do I retrieve inputs?

End Sub

Upvotes: 0

Views: 4434

Answers (3)

Matt Rowles
Matt Rowles

Reputation: 8050

Thanks to everyone for the help, it was much appreciated. Here is the code that ended up working:

//Create the XDoc object
Dim XMLDoc As XDocument

XMLDoc = New XDocument(
    New XDeclaration("1.0", "utf-8", "yes"),
        New XElement("user",
            New XElement("details",
                New XElement("firstname", Firstname.Text),
                    New XElement("surname", Lastname.Text)
            )
        )
    )

    //Save test file
    XMLDoc.Save("C:\test.xml")

Upvotes: 0

Nelson Miranda
Nelson Miranda

Reputation: 5554

public void Submit_Click(object sender, System.EventArgs e)
{
   //Get the inputs
   var name = Firstname.Text;
   var surname = TextBox1.Text;

   //Now you transform the data as the example of the link you showed
   XmlWriterSettings settings = new XmlWriterSettings();
   settings.Indent = true;
   settings.IndentChars = ("    ");

   var filepath = "data.xml";

   using (XmlWriter writer = XmlWriter.Create(filepath, settings))
   {
       // Write XML data.
       writer.WriteStartElement("data");
       writer.WriteElementString("name", name);
       writer.WriteElementString("surname", surname);
       writer.WriteEndElement();
       writer.Flush();
   }

   //Send XML file
   FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(FTPAddress + "/" + 
   Path.GetFileName(filePath));
   request.Method = WebRequestMethods.Ftp.UploadFile;
   request.Credentials = new NetworkCredential(username, password);
   request.UsePassive = true;
   request.UseBinary = true;
   request.KeepAlive = false;

   FileStream stream = File.OpenRead(filePath);
   byte[] buffer = new byte[stream.Length];
   stream.Read(buffer, 0, buffer.Length);
   stream.Close();

   Stream reqStream = request.GetRequestStream();
   reqStream.Write(buffer, 0, buffer.Length);
   reqStream.Close();
}

Include the System.Net library.

I wrote in notepad, didn't compile it, so forgive if there's an error, but this is the path.

Upvotes: 1

kappasims
kappasims

Reputation: 124

I would parse the form and reconstruct using LINQ to XML. I know it doesn't sound like much help but you're probably better off getting pointed in that direction (XDocument, XElement, etc.) and working with those .NET datatypes in an object-oriented fashion versus relying on a specific implementation (like @John Saunders said, not really sure about XMLBuilder) to fulfill your business need. It really pays to work under the hood sometimes.

Upvotes: 1

Related Questions