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

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

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

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

        XmlReader reader = XmlReader.Create("Customer2.xml", settings);
        List<CustomerInfo> lists = new List<CustomerInfo>();
        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();

原文地址:http://blog.51cto.com/13758648/2124257

时间: 2024-10-12 11:03:50

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

springbatch操作XML文件

一.需求分析 使用Spring Batch对XML文件进行读写操作: 从一个xml文件中读取商品信息, 经过简单的处理, 写入另外一个xml文件中. 二.代码实现 1. 代码结构图: 2. applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"

利用XmlDocument操作XML文件

利用XmlDocument可以方便的操作XML文件. 1.操作XML文件基本方法 (1)添加对System.Xml的引用,并使用using语句添加引用: (2)假设要读取的XML文件如下: <?xml version="1.0" encoding="utf-8"?> <Students> <Student> <Name>张靓靓</Name> <Age>20</Age> <Hob

Java操作XML文件 dom4j 篇

在项目中,我们很多都用到了xml文件,无论是参数配置还是与其它系统的数据交互.今天就来讲一下Java 中使用dom4j来操作XML文件. 我们需要引入的包: //文件包 import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileWriter; //工具包 import java.util.Iterator; import java.util.List; //dom4j包 import org.dom

.Net常用技巧_操作xml文件教程(插入节点、修改、删除)

已知有一个XML文件(bookstore.xml)如下:     <?xml   version="1.0"   encoding="gb2312"?>     <bookstore>         <book   genre="fantasy"   ISBN="2-3631-4">             <title>Oberon's   Legacy</title&

PHP操作XML文件学习笔记

原文:PHP操作XML文件学习笔记 XML文件属于标签语言,可以通过自定义标签存储数据,其主要作用也是作为存储数据. 对于XML的操作包括遍历,生成,修改,删除等其他类似的操作.PHP对于XML的操作方式很多,这次学习的是通过DOMDocument进行操作,其他的操作方法可以参考 http://www.oschina.net/code/snippet_110138_4727 1.对XML文件的遍历 通过DOMDocument对于XML文件的操作的方法:首先要实例化一个DOMDocument类的对

c#操作XML文件的通用方法

c#操作XML文件的通用方法 本文导读:我们在编写C#程序时,经常会通过C#访问XML文件,实现对XML文档的读写操作.下面为大家列出了通用的调用方法,大家可以将这些方法放在共用类里,其它的程序共享调用就可以了. 下面通过一个类将我们平时用c#操作XML文件的通用方法详细的介绍一下,关于asp.net C#操作xml文档实现代码,大家可以参考参考. c# 代码 1 sing System; 2 using System.Data; 3 using System.Configuration; 4

Asp.Net 操作XML文件的增删改查 利用GridView

不废话,直接上如何利用Asp.NET操作XML文件,并对其属性进行修改,刚开始的时候,是打算使用JS来控制生成XML文件的,但是最后却是无法创建文件,读取文件则没有使用了 index.aspx 文件 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="XmlManager.index" %> <!

C#操作XML文件实例汇总

针对XML文件的操作是C#程序设计中非常常见的功能.本文即以实例展示了C#操作XML文件的几个常见的示例.具体如下: 1.返回节点下标 public static XmlDocument getDoc(String path)//加载xml文档 { XmlDocument doc = new XmlDocument(); doc.Load(path); return doc; } /// <summary> /// 返回找到的节点下标 /// </summary> /// <

使用dom4j操作xml文件的增删改

package day2.domx; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter;