请问:在C#环境下,如何创建文件,并把一个字符串写入文件。 我得到了一个字符串,string xmlstring,想把它写入一个文件user.xml中,需要创建文件user.xml,再写入。请问该如何写呢?需要什么命名空间谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 创建一个XML文件XmlTextWriter m_xmlWriter = new XmlTextWriter(m_strFileName,System.Text.Encoding.Default); m_xmlWriter.WriteStartDocument(true); m_xmlWriter.WriteStartElement(STR_XMLROOT); m_xmlWriter.Close(); 保存到XML文件public string SaveToXML(string p_strUid,string p_strMailBody) { string m_strFile = GetXMLPath(p_strUid); string m_strGUID = STR_NODEHEADER + Guid.NewGuid().ToString(); XmlDocument m_xmlDom = new XmlDocument(); try { m_xmlDom.Load(m_strFile); XmlNode m_newNode = m_xmlDom.CreateElement(string.Empty,m_strGUID,string.Empty); m_newNode.InnerText = p_strMailBody; m_xmlDom.DocumentElement.AppendChild(m_newNode); m_xmlDom.Save(m_strFile); m_xmlDom = null; } catch(XmlException expXML) { m_xmlDom = null; throw new CustomException(STR_ERR_XML,expXML); } catch(Exception exp) { m_xmlDom = null; throw new CustomException(exp); } return m_strGUID; } public static void fileWrite(string strPath,string strInPut) { try { TextWriter output=File.AppendText(strPath); //文件中添加字符串 output.Write(strInPut);// output.WriteLine(strInPut); output.Close(); } catch { CommonFunc.sysMsgShow("71005"); // MessageBox.Show("写文件错误!"); } } 谢谢hjf1223和pasanal。 我明白了 哎,一个搞不懂的问题,关于 SQL2005服务器资源管理器 在那呀 WCF的问题 树形菜单的问题!!!怎么控制左边的树???? C#中关于验证码的问题 用c#来写一个程序生成器 怎么在datagridview中每列中添加combobox控件 调用API的问题 关于控件透明问题 请timmy3310兄解答 64位的xp支持。net framework 吗??任务管理器的多行多列控件是什么呀?? 初学者关于线程的疑问
XmlTextWriter m_xmlWriter = new XmlTextWriter(m_strFileName,System.Text.Encoding.Default);
m_xmlWriter.WriteStartDocument(true);
m_xmlWriter.WriteStartElement(STR_XMLROOT);
m_xmlWriter.Close();
public string SaveToXML(string p_strUid,string p_strMailBody)
{
string m_strFile = GetXMLPath(p_strUid);
string m_strGUID = STR_NODEHEADER + Guid.NewGuid().ToString();
XmlDocument m_xmlDom = new XmlDocument();
try
{
m_xmlDom.Load(m_strFile);
XmlNode m_newNode = m_xmlDom.CreateElement(string.Empty,m_strGUID,string.Empty);
m_newNode.InnerText = p_strMailBody;
m_xmlDom.DocumentElement.AppendChild(m_newNode);
m_xmlDom.Save(m_strFile);
m_xmlDom = null;
}
catch(XmlException expXML)
{
m_xmlDom = null;
throw new CustomException(STR_ERR_XML,expXML);
}
catch(Exception exp)
{
m_xmlDom = null;
throw new CustomException(exp);
}
return m_strGUID;
}
{
try
{
TextWriter output=File.AppendText(strPath); //文件中添加字符串
output.Write(strInPut);
// output.WriteLine(strInPut);
output.Close();
}
catch
{
CommonFunc.sysMsgShow("71005");
// MessageBox.Show("写文件错误!");
}
}