我想在C#中用XML来保存一起文本数据,会的高手帖点代码上来,谢谢!!

解决方案 »

  1.   

     DataSet ds = new DataSet("XMLProducts");
                string source = Login.Connection;//连接数据库
                SqlConnection conn = new SqlConnection(source);
                SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Products", conn);
                da.Fill(ds, "products");
                ds.WriteXml("E:\\练习\\ReadXml\\ReadXml\\sample.xml", XmlWriteMode.WriteSchema);这一断是把数据库里的字段保存到xml中
      

  2.   

    我的意思不是把数据库的字段保存到XML中而是把C#中里的TEXTBOX中的值保存到XML中,谁帮帮忙,谢谢
      

  3.   


    public EDSYSRtn SaveSetting()
    {
    XmlDocument cXMLDoc = new XmlDocument(); try
    {
    if (File.Exists(strFullPath) == false)
    ConstructXMLFile(ref cXMLDoc);
    else
    cXMLDoc.Load(strFullPath); for (int i = 0; i < cXMLDoc.FirstChild.ChildNodes.Count; i++)
    {
    XmlNode cTheNode = cXMLDoc.FirstChild.ChildNodes[i]; if (cXMLDoc.FirstChild.ChildNodes[i].Name == GeneralOption.NodeName)
    {
           // ...
    }
    if (cXMLDoc.FirstChild.ChildNodes[i].Name == DBOption.NodeName)
    {
           // ...
    }
    if (cXMLDoc.FirstChild.ChildNodes[i].Name == SolutionOption.NodeName)
    {
           // ...
    }
    } cXMLDoc.Save(strFullPath);
    return EDSYSRtn.Ok;
    }
    catch (Exception e)
    {
    string strMsg = e.Message;
    return EDSYSRtn.Fail;
    }
    }
    protected EDSYSRtn ConstructXMLFile(ref XmlDocument cXMLDoc)
    {
    cXMLDoc.AppendChild(cXMLDoc.CreateElement(TextBox1.Text));
    XmlNode cGeneralInfoNode = cXMLDoc.CreateElement(TextBox2.Text);
    cXMLDoc.FirstChild.AppendChild(cGeneralInfoNode); return EDSYSRtn.Ok;
    }