C#对XML文件的读写

将Ini文件存储换成XML文件保存(此处和ini文件一样保存数据库同步软件的相关信息)

这是一个和书店相关的XML写入方法:

 

 

//XML文件写方法
        //编写XML文件的格式并存储到指定的FilePath(路径)
        internal void WriterXML(string FilePath) {
           
            try {
                XmlDocument doc = new XmlDocument();//声明一个XmlDocument作为XML文档的容器
                //XmlDeclaration xmlDec = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
                XmlElement BookStory = doc.CreateElement("BookStory");//XMLElement表示一个标记或者元素的开始
                XmlAttribute Address = doc.CreateAttribute("Address");//XmlAttribute表示一个标记或者元素的属性
                Address.InnerText = "福建省厦门市集美新馆";//InnerText表示某一元素的内容
                BookStory.SetAttribute("Type", "On_Line");
                BookStory.SetAttribute("CreatTime", DateTime.Now.ToLongTimeString());
                XmlElement Book = doc.CreateElement("Book");
                XmlElement Tittle = doc.CreateElement("Tittle");
                Tittle.SetAttribute("Type", "BooksName");
                Tittle.InnerText = "C#7.0核心技术指南";
                XmlElement Price = doc.CreateElement("Price");
                Price.SetAttribute("Type", "RMB");
                Price.InnerText = "259.00";
                //doc.AppendChild(xmlDec);
                doc.AppendChild(BookStory);
                BookStory.AppendChild(Book);
                BookStory.Attributes.Append(Address);
                Book.AppendChild(Tittle);
                Book.AppendChild(Price);
                XmlWriterSettings settings = new XmlWriterSettings();//指定XMLWriter类实现的功能
                settings.Indent = true;
                settings.Encoding = Encoding.UTF8;//定义编码格式
                XmlWriter writer = XmlWriter.Create(FilePath, settings);//Create 方法使用 XmlWriterSettings 类指定要在创建的 XmlWriter 对象中实现哪些功能。
                doc.WriteTo(writer);
                writer.Flush();//清除缓存区
                writer.Close();
                MessageBox.Show("This Ok!");
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }
View Code

 

结果截图:

 

 

 

以下是对XML文件读写的学习以后根据自己的需求的衍生:

该图是XML文件写入格式以及相关数据的方法:

 

        //XML文件写入数据库同步的基础数据以及XML设计格式
        internal void WriterInfo(string FilePath) {
            XmlDocument doc = new XmlDocument();
            XmlElement XML = doc.CreateElement("XML");
            XmlElement Conn = doc.CreateElement("Conn");
            Conn.SetAttribute("Name","Conn1");
            Conn.SetAttribute("Type", "SQL Server数据库");
            XmlElement Source = doc.CreateElement("Source");
            XmlElement SServer = doc.CreateElement("SServer");
            XmlElement SIdentity = doc.CreateElement("SIdentity");
            XmlElement SDataBase = doc.CreateElement("SDataBase");
            XmlElement SUserID = doc.CreateElement("SUserID");
            XmlElement SPwd = doc.CreateElement("SPwd");
            XmlElement Target = doc.CreateElement("Target");
            XmlElement TServer = doc.CreateElement("TServer");
            XmlElement TIdentity = doc.CreateElement("TIdentity");
            XmlElement TDataBase = doc.CreateElement("TDataBase");
            XmlElement TUserID = doc.CreateElement("TUserID");
            XmlElement TPwd = doc.CreateElement("TPwd");
            doc.AppendChild(XML);
            XML.AppendChild(Conn);
            Conn.AppendChild(Source);
            Conn.AppendChild(Target);
            Source.AppendChild(SServer);
            Source.AppendChild(SIdentity);
            Source.AppendChild(SDataBase);
            Source.AppendChild(SUserID);
            Source.AppendChild(SPwd);
            Target.AppendChild(TServer);
            Target.AppendChild(TIdentity);
            Target.AppendChild(TDataBase);
            Target.AppendChild(TUserID);
            Target.AppendChild(TPwd);
            SServer.InnerText = "123.123.123";
            TServer.InnerText = "00000000";
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.Encoding = Encoding.UTF8;
            XmlWriter writer = XmlWriter.Create(FilePath,settings);
            doc.WriteTo(writer);
            writer.Flush();
            writer.Close();
            MessageBox.Show("It's Ok!");
        }

 该图是读取相关数据

 

//XML读取数据(数据库同步相关数据)
        /// <summary>
        /// 返回一个SQLModel的类集合
        /// </summary>
        /// <param name="FilePath">路径</param>
        /// <returns></returns>
        internal List<SQLModel> XMLReaders(string FilePath) {
            List<SQLModel> XMLInfo = new List<SQLModel>();
            XmlDocument doc = new XmlDocument();
            doc.Load(FilePath);
            XmlNode node1 = doc.SelectSingleNode("XML");
            /*XmlNode node2 = node1.SelectSingleNode("Conn");
            XmlNode node3 = node2.SelectSingleNode("Source");*/
            XmlNodeList node = node1.ChildNodes;//node = Conn1,Conn2....
            foreach (XmlNode item in node) {//this level is Conn
                SQLModel sQL = new SQLModel();
                XmlElement xml = (XmlElement)item;//item = one of Conn = xml 
                sQL.ConnName = xml.GetAttribute("Name").ToString();
                sQL.Type = xml.GetAttribute("Type").ToString();
                XmlNodeList Second = item.ChildNodes;//Second =  Source and Target
                foreach (XmlNode Snode in Second) {
                    if (Snode.Name == "Source") {
                        XmlNodeList SnodeChild = Snode.ChildNodes;
                        sQL.SServer = SnodeChild.Item(0).InnerText;
                        sQL.SIdentity = SnodeChild.Item(1).InnerText;
                        sQL.SDataBase = SnodeChild.Item(2).InnerText;
                        sQL.SUserID = SnodeChild.Item(3).InnerText;
                        sQL.SPwd = SnodeChild.Item(4).InnerText;
                    }
                    if (Snode.Name == "Target") {
                        XmlNodeList SnodeChild = Snode.ChildNodes;
                        sQL.TServer = SnodeChild.Item(0).InnerText;
                        sQL.TIdentity = SnodeChild.Item(1).InnerText;
                        sQL.TDataBase = SnodeChild.Item(2).InnerText;
                        sQL.TUserID = SnodeChild.Item(3).InnerText;
                        sQL.TPwd = SnodeChild.Item(4).InnerText;
                    }
                }
                XMLInfo.Add(sQL);
            }
            return XMLInfo;
        }
View Code

结果截图:

 

 

 

 

   以下是个人见解:

    目前对比下XML以及Ini两种格式存储数据,发现Ini用来存储类型表格的模式就较为繁杂,若是要存储树形的数据就更为费劲了;XML可以存储树形的数据,功能也更为强大,还支持LinQ,这也是广大程序员选择XML的原因。后续还会对Json进行相关的学习和交流。

posted @ 2020-07-15 12:06  Kai_YoungMaster  阅读(743)  评论(0编辑  收藏  举报