我想在程序的初始化阶段,创建一个.xml文件,用以存放程序中初始化的数值!!问一下,如何动态的在一个地方创建一个.xml文件呀???
比如说我想创建在e盘下,一个名为123.xml的文件? string str= @"e:\123.xml";
 FileStream filestream = new FileStream(str); XmlDocument xdoc = new XmlDocument();
 xdoc.Load(filestream);我搞到这里不太明白了。谁有这样类私的代码。或是方法呀。顶着有分。嘎嘎

解决方案 »

  1.   


     System.IO.File.Create(@"e:\123.xml"); //这样创建了一个空的 xml
     //xml 文件的结构要自己写进去。
      

  2.   

                StreamWriter sw = new StreamWriter(@"e:\123.xml",true, System.Text.Encoding.UTF8);
                sw.WriteLine("<?xml version=\"1.0\" encoding=\"" + "UTF-8" + "\" standalone=\"yes\"?>");
                sw.Write("</root>\r\n");
                sw.Close();
      

  3.   

    XmlDocument xdoc = new XmlDocument(); 
    //填写xml 文件的结构
    xdoc.Save((@"e:\123.xml");
    这样就生成了一个xml文件。
      

  4.   

    #region 创建保存保存信息的临时XML
    /// <summary>
    /// 创建保存保存信息的临时XML
    /// </summary>
    /// <param name="strSaveName"></param>
    /// <param name="strSavePath"></param>
    /// <param name="strNotSaveName"></param>
    /// <param name="strNotSavePath"></param>
    public void CreateXML(string strSaveName,string strSavePath,string strNotSaveName,string strNotSavePath)
    {
    string[] arrSaveName = strSaveName.Split('$');
    string[] arrSavePath = strSavePath.Split('$');
    string[] arrNotSaveName = null;
    string[] arrNotSavePath = null;
    int iSaveCount = strSaveName.Split('$').Length-1;
    int iNotSaveCount = 0; XmlDocument oXml = new XmlDocument();
    XmlDeclaration oDeclare = oXml.CreateXmlDeclaration("1.0", "GB2312", null);
    oXml.AppendChild(oDeclare);
    //创建一个根节点(一级)
    XmlElement oRoot = oXml.CreateElement("SaveInfo");
    oXml.AppendChild(oRoot);
    //创建保存图片信息节点
    XmlNode oNodeSaveName = oXml.CreateElement("SaveName");
    XmlNode oNodeSavePath = oXml.CreateElement("SavePath");
    XmlNode oNotSaveName = oXml.CreateElement("NotSaveName");
    XmlNode oNotSavePath = oXml.CreateElement("NotSavePath");
    //创建已经保存信息
    try
    {
    for(int i=0 ; i<iSaveCount ; i++)
    {
    XmlElement oElementName= oXml.CreateElement("Name"+i.ToString());
    oElementName.SetAttribute("Name", "NameC"+i.ToString());
    oElementName.SetAttribute("ID", "NamePC"+i.ToString());
    oElementName.InnerText = "@"+arrSaveName[i];
    oNodeSaveName.AppendChild(oElementName); XmlElement oElementPath= oXml.CreateElement("Path"+i.ToString());
    oElementPath.SetAttribute("Name", "PathC"+i.ToString());
    oElementPath.SetAttribute("ID", "PathPC"+i.ToString());
    oElementPath.InnerText = arrSavePath[i].Insert(arrSavePath[i].LastIndexOf("\\")+1,"@");
    oNodeSavePath.AppendChild(oElementPath);
    }
    }
    catch(Exception e)
    {
    string strError = e.Message;
    } oRoot.AppendChild(oNodeSaveName);
    oRoot.AppendChild(oNodeSavePath); if(strNotSaveName != string.Empty)
    {
    arrNotSaveName = strNotSaveName.Split('$');
    arrNotSavePath = strNotSavePath.Split('$');
    iNotSaveCount  = arrNotSaveName.Length-1;
    try
    {
    for(int i=0 ; i<iNotSaveCount ; i++)
    {
    XmlElement oElementNotName= oXml.CreateElement("NotName"+i.ToString());
    oElementNotName.SetAttribute("Name", "NotNameC"+i.ToString());
    oElementNotName.SetAttribute("ID", "NotNamePC"+i.ToString());
    oElementNotName.InnerText = "@"+arrNotSaveName[i];
    oNotSaveName.AppendChild(oElementNotName); XmlElement oElementNotPath= oXml.CreateElement("NotPath"+i.ToString());
    oElementNotPath.SetAttribute("Name", "NotPathC"+i.ToString());
    oElementNotPath.SetAttribute("ID", "NotPathPC"+i.ToString());
    oElementNotPath.InnerText = arrNotSavePath[i].Insert(arrNotSavePath[i].LastIndexOf("\\")+1,"@");
    oNotSavePath.AppendChild(oElementNotPath);
    }
    oRoot.AppendChild(oNotSaveName);
    oRoot.AppendChild(oNotSavePath);
    }
    catch(Exception e)
    {
    string str = e.Message;
    }
    } try
    {
    string strXMLPath = HttpContext.Current.Server.MapPath("../Plsctp/")+"Temp.xml";
    oXml.Save(strXMLPath);
    Console.Write(oXml.OuterXml);
    }
    catch(Exception e)
    {
    string str = e.Message;
    }
    }
    #endregion
      

  5.   

    按照需求来看用序列化会很方便,给你关键方法
            public static bool  SerializeInput(Input input, string path)
            {
                try
                {
                    XmlSerializer formatter = new XmlSerializer(typeof(Input));
                    Stream stream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
                    formatter.Serialize(stream, input);
                    stream.Close();
                }
                catch
                {
                    return false;
                }
                return true;
            }        public static Input DeserializeInput(string path)
            {
                XmlSerializer fomatter = new XmlSerializer(typeof(Input));
                Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None);
                stream.Seek(0, SeekOrigin.Begin);
                Input input = (Input)fomatter.Deserialize(stream);
                stream.Close();
                return input;
            }    public partial class Input
        {
            private string ed1 = string.Empty;
            public string ExcelDir1
            {
                set
                {
                    ed1 = value;
                }
                get
                {
                    return ed1;
                }
            }        private string ed2 = string.Empty;
            public string ExcelDir2
            {
                set { ed2 = value; }
                get { return ed2; }
            }        private string ed3 = string.Empty;
            public string ExcelDir3
            {
                set { ed3 = value; }
                get { return ed3; }
            }        private string ed4 = string.Empty;
            public string ExcelDir4
            {
                set
                {
                    ed4 = value;
                }
                get
                {
                    return ed4;
                }
            }        private string ed5 = string.Empty;
            public string ExcelDir5
            {
                set { ed5 = value; }
                get { return ed5; }
            }        private string cd1 = string.Empty;
            public string ConfigDir1
            {
                set
                {
                    cd1 = value;
                }
                get
                {
                    return cd1;
                }
            }        private string cd2 = string.Empty;
            public string ConfigDir2
            {
                set { cd2 = value; }
                get { return cd2; }
            }        private string cd3 = string.Empty;
            public string ConfigDir3
            {
                set { cd3 = value; }
                get { return cd3; }
            }        private string cd4 = string.Empty;
            public string ConfigDir4
            {
                set
                {
                    cd4 = value;
                }
                get
                {
                    return cd4;
                }
            }        private string cd5 = string.Empty;
            public string ConfigDir5
            {
                set { cd5 = value; }
                get { return cd5; }
            }
        }
      

  6.   

    PC端的许多初始化参数需要保存,我对PC机的编程是很怵的。
    还是在编WM6.0下的通讯软件。使用VS2005下的C#开发。
    初始化参数我见到的有这么几种。
    1、使用ini文件:好像说是.net不推荐使用,就没有深入学习了。
    2、使用注册表。这是MS推荐使用的,但俺怕麻烦。觉得还是绿色软件好一些。
    3、使用XML。.net对XML文件的支持力度很强。俺决心就用它了。我的XML文件是这样的。
    <?xml version="1.0" encoding="utf-8"?>
    <Modbus Version="1.00.00">
      <CommPort>COM1</CommPort>
      <MassUnit>T</MassUnit>
    </Modbus>
    编程思路是首先读取ModbusIni.xml,如果文件不存在则创建这个文件,如果文件存在则读取文件中的参数,最后在程序关闭时保存设置好的参数到ModbusIni.xml。
    首先定义两个变量,mDocument代表XML文件,mCurrentNode表示选择好的节点。
            private XmlDocument mDocument;
            private XmlNode mCurrentNode;
            public Form1()
            {
                InitializeComponent();
                foreach (string s in SerialPort.GetPortNames())
                {
                    comboBox1.Items.Add(s);   //通讯端口的选择
                }
         
                bool bSave = false;
                mDocument = new XmlDocument();
                try
                {
                    mDocument.Load(@"ModbusIni.xml");//判断文件Load正确否
                }
                catch
                {
                    // 走到这里就失败了,文件不存在或其它错误
                    bSave = true;
                    CreatXmlDocument();//创建文件
                }
                if (!bSave)
                {
                    ReadXmlDocument();
                }
            }        private void CreatXmlDocument()               //建立新的文件          
            {
                //定义XML文档头文件
                XmlDeclaration xmlDeclaration = mDocument.CreateXmlDeclaration("1.0", "utf-8", null);
                //增加XML文档头
                mDocument.AppendChild(xmlDeclaration);
                //定义XML的根
                XmlElement xmlRoot = mDocument.CreateElement("Modbus");
                //添加XML的根
                mDocument.AppendChild(xmlRoot);
                //添加根的属性
              xmlRoot.SetAttribute("Version", "1.00.00");
              XmlElement newCommPort = mDocument.CreateElement("CommPort");
              XmlElement newMassUnit = mDocument.CreateElement("MassUnit");
         
                XmlText CommPort = mDocument.CreateTextNode("COM1");
                XmlText MassUnit = mDocument.CreateTextNode("T");            newCommPort.AppendChild(CommPort);
                newMassUnit.AppendChild(MassUnit);            //添加XML根的节点
                xmlRoot.AppendChild(newCommPort);
                xmlRoot.AppendChild(newMassUnit);            // Save the document to a file.
                mDocument.Save("ModbusIni.xml");
            }
            private void ReadXmlDocument()               //读取Xml文件
            {
                XmlElement xmlRoot = mDocument.DocumentElement;
              //使用SelectSingleNode查找到想要的参数
                mCurrentNode = xmlRoot.SelectSingleNode("CommPort");
                comboBox1.Text = mCurrentNode.InnerText;        }
            private void SaveXmlDocument()               //保存Xml文件
            {
                XmlElement xmlRoot = mDocument.DocumentElement;
    //使用SelectSingleNode查找到想要的参数,
                mCurrentNode = xmlRoot.SelectSingleNode("CommPort");
                mCurrentNode.InnerText = comboBox1.Text ;            mDocument.Save("ModbusIni.xml");
            }        //程序退出时保存XML文件
            private void Form1_Closed(object sender, EventArgs e)
            {
                SaveXmlDocument();
            }
      

  7.   

    创建和保存一个XML文件有必要搞那么大堆代码?
    用Linq;
    XDocument doc = new XDocument();
    doc.Save((@"e:\123.xml");