用XTW与XTR访问创建和读取XML文件

ZDNet软件频道 时间:2008-07-10 作者:闵刚 | 中国IT实验室 我要评论()
本文关键词:读取 创建 XML 软件
用XTW与XTR访问创建和读取XML文件
using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Text;
        using System.Windows.Forms;
        using System.XML;
        namespace XMLSAXDemo
        {
            public partial class Form1 : Form
            {
                public Form1()
                {
                    InitializeComponent();
                }
                private void btnWrite_Click(object sender, EventArgs e)
                {
                    string fileName = "Books.XML";
                    XMLTextWriter writer = new XMLTextWriter(fileName, null);
                    writer.Formatting = Formatting.Indented;
                    writer.Indentation = 6;
                    writer.WriteStartDocument();
                    writer.WriteStartElement("Books");
                    writer.WriteStartElement("Book");
                    writer.WriteElementString("Title", "Window Form");
                    writer.WriteElementString("ISBN", "111111");
                    writer.WriteElementString("Author", "amandag");
                    writer.WriteStartElement("Price");
                    writer.WriteAttributeString("Unit", "¥");
                    writer.WriteValue("128.00");
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                    writer.WriteStartElement("Book");
                    writer.WriteElementString("Title", "ASP.NET");
                    writer.WriteElementString("ISBN", "222222");
                    writer.WriteElementString("Author", "moon");
                    writer.WriteStartElement("Price");
                    writer.WriteAttributeString("Unit", "___FCKpd___0quot;);
                    writer.WriteValue("111.00");
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                    writer.WriteEndDocument();
                    writer.Close();
                }
                private void btnRead_Click(object sender, EventArgs e)
                {
                    string fileName = "Books.XML";
                    XMLTextReader reader = new XMLTextReader(fileName);
                    string str = string.Empty;
                    while (reader.Read())
                    {
                        if (reader.NodeType == XMLNodeType.Element)
                        {
                            if (reader.LocalName.Equals("Title") || reader.LocalName.Equals("Author"))
                            {
                                str += reader.ReadString() + " ";
                            }
                        }
                    }
                    MessageBox.Show(str);
                }
            }
        }

读取

创建

XML

软件


百度大联盟认证黄金会员Copyright© 1997- CNET Networks 版权所有。 ZDNet 是CNET Networks公司注册服务商标。
中华人民共和国电信与信息服务业务经营许可证编号:京ICP证010391号 京ICP备09041801号-159
京公网安备:1101082134