C# XmlTextWriter和XmlTextReader 读写XML文件

2018-06-18 04:57:15来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
 
namespace Xml
{
    class Program
    {
        static void Main(string[] args)
        {
            // XmlTextWriter 写文件
            XmlTextWriter writeXml = new XmlTextWriter(@c:MyXml.xml,Encoding.UTF8);
            writeXml.WriteStartDocument(false);
            writeXml.WriteStartElement(NetWork);
  
            writeXml.WriteComment(网络配置信息);
            writeXml.WriteStartElement(configration);
  
            writeXml.WriteElementString(IpAddress,192.168.2.168);
            writeXml.WriteElementString(Netmask, 255.255.255.0);
            writeXml.WriteElementString(Gateway, 202.103.24.68);
  
            writeXml.WriteEndElement();
            writeXml.WriteEndElement();
  
            writeXml.Flush();
            writeXml.Close();
 
 
            // XmlTextReader 读文件
            XmlTextReader readerXml = new XmlTextReader(@c:MyXml.xml);
            while(readerXml.Read())
            {
                if(readerXml.NodeType == XmlNodeType.Element)
                {
 
                    if(readerXml.Name == IpAddress)
                    {
                        Console.WriteLine(readerXml.ReadElementString().Trim());
                    }
                    if (readerXml.Name == Netmask)
                    {
                        Console.WriteLine(readerXml.ReadElementString().Trim());
                    }
                    if (readerXml.Name == Gateway)
                    {
                        Console.WriteLine(readerXml.ReadElementString().Trim());
                    }
                }
            }
            Console.ReadKey();
        }
    }
}

Xml文件内容:

\

控制台运行结果:

\

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:五分钟搭建起一个包含CRUD功能的JqGrid表格

下一篇:mysql通用分页存储过程遇到的问题