基本上我想要的是:
我的主要问题是无法找到有关 XMLBuilder 的更多信息。以下链接看起来像我需要的东西,但我只能使用XML Builder:Creating a Contact form in Visual Studio ASPX and saving to an XML file when clicking SUBMIT
我的代码如下:
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> <asp:TextBox ID="Firstname" runat="server"></asp:TextBox><br />
<asp:Label runat="server">Surname</asp:Label> <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
答案 0 :(得分:1)
我将使用LINQ to XML解析表单并重构。我知道这听起来不是很有帮助,但你可能最好指向那个方向(XDocument,XElement等)并以面向对象的方式使用这些.NET数据类型而不是依赖于特定的实现(像@John Saunders所说,不太确定XMLBuilder)来满足您的业务需求。有时真的值得工作。
答案 1 :(得分:1)
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();
}
包括System.Net库。
我在记事本中写道,没有编译它,所以原谅如果有错误,但这就是路径。
答案 2 :(得分:0)
感谢大家的帮助,非常感谢。以下是最终工作的代码:
//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")