我得到了一个字符串,string xmlstring,想把它写入一个文件user.xml中,需要创建文件user.xml,再写入。请问该如何写呢?需要什么命名空间谢谢

解决方案 »

  1.   

    创建一个XML文件
    XmlTextWriter m_xmlWriter = new XmlTextWriter(m_strFileName,System.Text.Encoding.Default);
    m_xmlWriter.WriteStartDocument(true);
    m_xmlWriter.WriteStartElement(STR_XMLROOT);
    m_xmlWriter.Close();
      

  2.   

    保存到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;
    }
      

  3.   

    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("写文件错误!");
    }
    }
      

  4.   

    谢谢hjf1223和pasanal。 我明白了