我创建了一个窗体 有两个对话框和一个确定按钮 现在要求在两个对话框中输入字符串 然后按确定键  就把这两个字符串存到一个Xml文挡中。本人不知道什么是xml文挡 应该这样创建 我怎么才能通过按确定键创建一个XML文挡 把字符串存进去呀。如果帮我搞定 我再加100分

解决方案 »

  1.   

    using System.Xml;参照System.Xml.XmlDocument吧。
      

  2.   

    也就是我在创建了一个windows的项目,然后在Form1中创建了2个TextBox和1个button  要求能够让用户在这两个textBox中输入字符串 然后按这个button 就能够把这两个字符串写到一个xml文档中
      

  3.   

    private void Button1_Click(object sender, System.EventArgs e)
    {
    XmlTextWriter wr=new XmlTextWriter(Server.MapPath("hehe.xml"),null);
    wr.WriteStartDocument();
    wr.WriteStartElement("persons");
    wr.WriteStartElement("person"); wr.WriteStartElement("username");
    wr.WriteString(this.TextBox1.Text);
    wr.WriteEndElement(); wr.WriteStartElement("password");
    wr.WriteString(this.TextBox2.Text);
    wr.WriteEndElement(); wr.WriteEndElement();
    wr.WriteEndElement();

    wr.WriteEndDocument();
    wr.Close();

    }
    在TextBox1输入testxml,TextBox2输入test
    然后生成 hehe.xml,内容如下:
    <?xml version="1.0">
    <persons>
     <person>
      <username>testxml</username>
      <password>test</password>
     </person>
    </persons>
      

  4.   

    /// <summary>
    /// 从xml文件中读取指定键的值
    /// </summary>
    /// <param name="strKey">键</param>
    /// <param name="strValue">值</param>
    /// <returns>返回指定键的值</returns>
    public static string ReadValue(string strKey)
    {
    System.IO.FileInfo FileInfo = new System.IO.FileInfo(文件路径);

    XmlDocument xmlDocument = new XmlDocument();
    xmlDocument.Load(FileInfo.FullName);
    foreach(XmlNode node in xmlDocument["configuration"]["appSettings"])
    {
    if(node.Attributes["key"].Value == strKey)
    {
    return node.Attributes["value"].Value;
    }
    }
    return "";
    }
    /// <summary>
    /// 对xml文件的指定键写入指定值
    /// </summary>
    /// <param name="strKey">键</param>
    /// <param name="strValue">值</param>
    public static void WriterValue(string strKey,string strValue)
    {
    System.IO.FileInfo  FileInfo= new  System.IO.FileInfo(xml文件路径);

    XmlDocument XmlDocument1 =new XmlDocument(); XmlDocument1.Load(FileInfo.FullName);

    foreach(XmlNode Node in XmlDocument1["configuration"]["appSettings"])
    {
    if(Node.Attributes["key"].Value == strKey)
    {
    Node.Attributes["value"].Value = strValue;
    }
    }
    XmlDocument1.Save(FileInfo.FullName);
    }
      

  5.   

    如果是windows项目,把这句
    XmlTextWriter wr=new XmlTextWriter(Server.MapPath("hehe.xml"),null);该为:
    XmlTextWriter wr=new XmlTextWriter(Application.StartupPath+"\\..\\..\\hehe.xml",null);
    就可以了
      

  6.   

    //xml查询测试
    using System;
    using System.IO;
    using System.Text;
    using System.Diagnostics;
    using System.Threading;
    using System.Collections;
    using System.Data;
    using System.Xml;
    using System.Management;
    using System.Net;
    namespace Zhzuo
    {
    class ZZConsole
    {
    [STAThread]
    static void Main(string[] args)
    {
    string strXml="<?xml version=\"1.0\"?>"
    +"<Data>"
    +"<Head>"
    +"<Nodeid>1111</Nodeid>"
    +"<Subid>2222</Subid>"
    +"<Version>2004</Version>"
    +"<Date>20040302</Date>"
    +"<Time>101500</Time>"
    +"</Head>"
    +"<Body>"
    +"<Code>01</Code>"
    +"<Name>深圳</Name>"
    +"<IdType>0</IdType>"
    +"<Idno>110258740824082</Idno>"
    +"</Body>"
    +"</Data>";
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(strXml);
    string vv;
    vv = doc.GetElementsByTagName("Version")[0].InnerText;
    Console.WriteLine(vv);
    vv = doc.SelectNodes("//Version")[0].InnerText;
    Console.WriteLine(vv);

    Console.ReadLine();
    }

    }

    }