如何引用文本框中包含的文本来创建 XML 文件?
我正在使用 Visual C++ 2005 .net,我希望从此表单创建一个文件。我希望能够通过输入文本框中的内容来命名该文件。任何帮助将不胜感激。
这是形式:
这是我到目前为止所拥有的:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
String^ path = "C:\\" ".txt" ;
StreamWriter^ sw = File::CreateText( path );
try
{
sw->WriteLine("");
}
finally
{
if ( sw )
delete (IDisposable^)sw;
}
}
};
固定代码:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
NewPart ^newpart = gcnew NewPart();
newpart->ShowDialog();
this->Close();
String^ fileName = textBox1->Text;
String^ filetype = ".xml";
String^ path = L"C:\\;
String^ fullName = path + fileName + filetype;
StreamWriter^ sw = File::CreateText(fullName);
try
{
sw->WriteLine("<?xml version= \"1.0\" standalone=\"yes\"?>");
sw->WriteLine("<macdata");
sw->WriteLine("</macdata>");
}
finally
{
if ( sw )
delete (IDisposable^)sw;
}
}
};
}
I am using visual c++ 2005 .net and I am looking to create a file from this form. I want to be able to name the file by entering what ever is in the text box. Any help would be greatly appreciated.
Here is the form:
this is what I have so far:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
String^ path = "C:\\" ".txt" ;
StreamWriter^ sw = File::CreateText( path );
try
{
sw->WriteLine("");
}
finally
{
if ( sw )
delete (IDisposable^)sw;
}
}
};
FIXED CODE:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
NewPart ^newpart = gcnew NewPart();
newpart->ShowDialog();
this->Close();
String^ fileName = textBox1->Text;
String^ filetype = ".xml";
String^ path = L"C:\\;
String^ fullName = path + fileName + filetype;
StreamWriter^ sw = File::CreateText(fullName);
try
{
sw->WriteLine("<?xml version= \"1.0\" standalone=\"yes\"?>");
sw->WriteLine("<macdata");
sw->WriteLine("</macdata>");
}
finally
{
if ( sw )
delete (IDisposable^)sw;
}
}
};
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 textBox1 替换为表单中的实际 TextBox 名称。
编辑。关于第二个问题,请看这段代码:
Replace textBox1 with actual TextBox name from your form.
Edit. Regarding your second question, look at this code: