C#操作XMl文件(2):使用XmlReader和XmlWriter实现读取和写入

这次使用操作Xml较为常用的方法:使用XMlreader和Xmlwriter

成都创新互联公司专注于企业全网营销推广、网站重做改版、福绵网站定制设计、自适应品牌网站建设、成都h5网站建设商城开发、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为福绵等各大城市提供网站开发制作服务。

1:读取xml文件的数学和元素

XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;
settings.IgnoreWhitespace = true;

        XmlReader reader = XmlReader.Create("Customer2.xml", settings);
        List lists = new List();
        CustomerInfo cust = null;

        while (reader.Read())
        {
            if (reader.NodeType == XmlNodeType.Element)
            {
                switch (reader.Name)
                {
                    case "row":
                        cust = new CustomerInfo();
                        if (reader.HasAttributes)
                        {
                            cust.AppId = reader.GetAttribute("AppID");
                            cust.Version = reader.GetAttribute("Version");
                        }
                        break;

                    case "CustomerID":
                        cust.CustomerID = reader.ReadString();
                        break;

                    case "CompanyName":
                        cust.CompanyName = reader.ReadString();
                        break;
                    case "ContactName":

                        cust.ContactName = reader.ReadString();
                        break;
                    case "ContactTitle":

                        cust.ContactTitle = reader.ReadString();
                        break;

                    case "Address":

                        cust.Address = reader.ReadString();
                        break;

                    case "City":
                        cust.City = reader.ReadString();
                        break;

                    case "PostalCode":
                        cust.PostalCode = reader.ReadString();
                        break;
                    case "Country":
                        cust.Country = reader.ReadString();
                        break;
                    case "Phone":
                        cust.Phone = reader.ReadString();
                        break;
                    case "Fax":
                        cust.Fax = reader.ReadString();
                        lists.Add(cust);
                        break;
                    default:
                        break;

                }
            }
        }

2.创建文档-属性和元素
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineOnAttributes = false;
XmlWriter writer = XmlWriter.Create("CustomerNewElementArribute.xml", settings);
writer.WriteStartDocument();
writer.WriteStartElement("Table");

        for (int i = 1; i < 10; i++)
        {
            writer.WriteStartElement("row");
            writer.WriteAttributeString("Version", "2.0");
            writer.WriteAttributeString("AppID", "111");
            writer.WriteElementString("CustomerID", "cmz" + i.ToString());
            writer.WriteElementString("CompanyName", "程沐喆" + i.ToString());
            writer.WriteElementString("ContactName", "Maria Anders" + i.ToString());
            writer.WriteElementString("ContactTitle", "Sales Representative" + i.ToString());
            writer.WriteElementString("Address", "Obere Str. 57" + i.ToString());
            writer.WriteElementString("City", "Berlin");
            writer.WriteElementString("PostalCode", "12209");
            writer.WriteElementString("Country", "Germany");
            writer.WriteElementString("Phone", "030-0074321");
            writer.WriteElementString("Fax", "030-0076545");
            writer.WriteEndElement();
        }

        writer.WriteEndElement();
        writer.WriteEndDocument();
        writer.Flush();
        writer.Close();

    }

    private void button7_Click(object sender, EventArgs e)
    {
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        settings.NewLineOnAttributes = false;
        settings.Encoding = Encoding.UTF8;
        StringBuilder builder = new StringBuilder();
        XmlWriter writer = XmlWriter.Create(builder, settings);
        writer.WriteStartDocument();
        writer.WriteStartElement("Table");

        for (int i = 1; i < 5; i++)
        {
            writer.WriteStartElement("row");
            writer.WriteAttributeString("Version", "2.0");
            writer.WriteAttributeString("AppID", "111");
            writer.WriteElementString("CustomerID", "cmz" + i.ToString());
            writer.WriteElementString("CompanyName", "程沐喆" + i.ToString());
            writer.WriteElementString("ContactName", "Maria Anders" + i.ToString());
            writer.WriteElementString("ContactTitle", "Sales Representative" + i.ToString());
            writer.WriteElementString("Address", "Obere Str. 57" + i.ToString());
            writer.WriteElementString("City", "Berlin");
            writer.WriteElementString("PostalCode", "12209");
            writer.WriteElementString("Country", "Germany");
            writer.WriteElementString("Phone", "030-0074321");
            writer.WriteElementString("Fax", "030-0076545");
            writer.WriteEndElement();
        }
        writer.WriteEndElement();
        writer.WriteEndDocument();
        writer.Flush();
        writer.Close();

另外在介绍自己学习的使用xmlwriter写入StringBuiler和Stream

创建xml写入StringBuiler:

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineOnAttributes = false;
settings.Encoding = Encoding.UTF8;
StringBuilder builder = new StringBuilder();
XmlWriter writer = XmlWriter.Create(builder, settings);
writer.WriteStartDocument();
writer.WriteStartElement("Table");

        for (int i = 1; i < 5; i++)
        {
            writer.WriteStartElement("row");
            writer.WriteAttributeString("Version", "2.0");
            writer.WriteAttributeString("AppID", "111");
            writer.WriteElementString("CustomerID", "ALFKI" + i.ToString());
            writer.WriteElementString("CompanyName", "Alfreds Futterkiste" + i.ToString());
            writer.WriteElementString("ContactName", "Maria Anders" + i.ToString());
            writer.WriteElementString("ContactTitle", "Sales Representative" + i.ToString());
            writer.WriteElementString("Address", "Obere Str. 57" + i.ToString());
            writer.WriteElementString("City", "Berlin");
            writer.WriteElementString("PostalCode", "12209");
            writer.WriteElementString("Country", "Germany");
            writer.WriteElementString("Phone", "030-0074321");
            writer.WriteElementString("Fax", "030-0076545");
            writer.WriteEndElement();
        }
        writer.WriteEndElement();
        writer.WriteEndDocument();
        writer.Flush();
        writer.Close();

创建xml写入stream:
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineOnAttributes = false;
settings.Encoding = new UTF8Encoding(false);
MemoryStream stream = new MemoryStream();
XmlWriter writer = XmlWriter.Create(stream, settings);
writer.WriteStartDocument();
writer.WriteStartElement("Table");

        for (int i = 1; i < 5; i++)
        {
            writer.WriteStartElement("row");
            writer.WriteAttributeString("Version", "2.0");
            writer.WriteAttributeString("AppID", "111");
            writer.WriteElementString("CustomerID", "ALFKI" + i.ToString());
            writer.WriteElementString("CompanyName", "Alfreds Futterkiste" + i.ToString());
            writer.WriteElementString("ContactName", "Maria Anders" + i.ToString());
            writer.WriteElementString("ContactTitle", "Sales Representative" + i.ToString());
            writer.WriteElementString("Address", "Obere Str. 57" + i.ToString());
            writer.WriteElementString("City", "Berlin");
            writer.WriteElementString("PostalCode", "12209");
            writer.WriteElementString("Country", "Germany");
            writer.WriteElementString("Phone", "030-0074321");
            writer.WriteElementString("Fax", "030-0076545");
            writer.WriteEndElement();
        }
        writer.WriteEndElement();
        writer.WriteEndDocument();
        writer.Flush();
        writer.Close();

分享题目:C#操作XMl文件(2):使用XmlReader和XmlWriter实现读取和写入
链接分享:http://bzwzjz.com/article/goocjc.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 成都网站建设公司 企业网站建设 营销型网站建设 成都企业网站制作 手机网站建设套餐 企业网站制作 手机网站制作 定制级高端网站建设 成都h5网站建设 网站设计 成都网站设计公司 重庆外贸网站建设 企业网站设计 古蔺网站建设 成都网站制作 网站设计 温江网站设计 做网站设计 泸州网站建设 网站建设公司 定制网站设计 成都网站制作