C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C#写XML文件

C#写入XML文档

作者:農碼一生

这篇文章介绍了C#写入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添加子节点
                BookStory.AppendChild(Book);
                BookStory.Attributes.Append(Address);
                //Book 添加子节点
                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);
            }
        }

写入内容:

<?xml version="1.0" encoding="utf-8"?>
<BookStory Type="On_Line" CreatTime="下午 06:04:50" Address="福建省厦门市集美新馆">
  <Book>
    <Tittle Type="BooksName">C#7.0核心技术指南</Tittle>
    <Price Type="RMB">259.00</Price>
  </Book>
</BookStory>

二、案例二

        //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);
            Source.AppendChild(SServer);
            SServer.InnerText = "123.123.123";
            Source.AppendChild(SIdentity);
            Source.AppendChild(SDataBase);
            Source.AppendChild(SUserID);
            Source.AppendChild(SPwd);

            Conn.AppendChild(Target);
            Target.AppendChild(TServer);
            TServer.InnerText = "00000000";
            Target.AppendChild(TIdentity);
            Target.AppendChild(TDataBase);
            Target.AppendChild(TUserID);
            Target.AppendChild(TPwd);
        
            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 version="1.0" encoding="utf-8"?>
<XML>
  <Conn Name="Conn1" Type="SQL Server数据库">
    <Source>
      <SServer>123.123.123</SServer>
      <SIdentity />
      <SDataBase />
      <SUserID />
      <SPwd />
    </Source>
    <Target>
      <TServer>00000000</TServer>
      <TIdentity />
      <TDataBase />
      <TUserID />
      <TPwd />
    </Target>
  </Conn>
</XML>

到此这篇关于C#写入XML文档的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文